$ node --version
v0.12.6
$ node index.js
Array: indexOf x 876,895 ops/sec ±0.95% (95 runs sampled)
Object with prototype: in operator x 18,518,152 ops/sec ±0.84% (94 runs sampled)
Object with prototype: value not undefined x 85,706,356 ops/sec ±0.75% (95 runs sampled)
Object with prototype: typeof value not undefined x 84,955,696 ops/sec ±0.95% (94 runs sampled)
Object without prototype: in operator x 18,283,395 ops/sec ±0.83% (93 runs sampled)
Object without prototype: value not undefined x 83,992,518 ops/sec ±1.43% (91 runs sampled)
Object without prototype: typeof value not undefined x 84,716,082 ops/sec ±0.80% (94 runs sampled)
Fastest is Object with prototype: value not undefined,Object with prototype: typeof value not undefined
Last active
August 29, 2015 14:24
-
-
Save aymericbeaumet/0b0b14550896a1a3c703 to your computer and use it in GitHub Desktop.
Benchmarking Array.prototype.indexOf and Object in operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var assert = require('assert'); | |
var benchmark = require('benchmark'); | |
var suite = new benchmark.Suite(); | |
var array = Array.apply(null, {length: 10000}).map(Number.call, Number); | |
var objectWithPrototype = array.reduce(function(acc, element) { acc[element] = null; return acc; }, {}); | |
var objectWithoutPrototype = array.reduce(function(acc, element) { acc[element] = null; return acc; }, Object.create(null)); | |
suite.add('Array: indexOf', function() { | |
assert(array.indexOf(999) > -1); | |
}); | |
suite.add('Object with prototype: in operator', function() { | |
assert('999' in objectWithPrototype); | |
}); | |
suite.add('Object with prototype: value not undefined', function() { | |
assert(objectWithPrototype['999'] !== undefined); | |
}); | |
suite.add('Object with prototype: typeof value not undefined', function() { | |
assert(typeof objectWithPrototype['999'] !== 'undefined'); | |
}); | |
suite.add('Object without prototype: in operator', function() { | |
assert('999' in objectWithoutPrototype); | |
}); | |
suite.add('Object without prototype: value not undefined', function() { | |
assert(objectWithoutPrototype['999'] !== undefined); | |
}); | |
suite.add('Object without prototype: typeof value not undefined', function() { | |
assert(typeof objectWithoutPrototype['999'] !== 'undefined'); | |
}); | |
suite | |
.on('cycle', function(event) { console.log(String(event.target)); }) | |
.on('complete', function() { console.log('Fastest is ' + this.filter('fastest').pluck('name')); }) | |
.run({ 'async': true }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependencies": { | |
"benchmark": "1.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment