Skip to content

Instantly share code, notes, and snippets.

@rumkin
Created June 21, 2016 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rumkin/f7542ead1dc754309811fe0ca2818183 to your computer and use it in GitHub Desktop.
Save rumkin/f7542ead1dc754309811fe0ca2818183 to your computer and use it in GitHub Desktop.
Compare objects with variadic property set versus Map
const Benchmark = require('benchmark');
const suite = new Benchmark.Suite;
// Test data set size
const n = process.env.SET_SIZE || 100000;
// Megamorphic objects array
const objects = Array(n);
// Array of map instances
const maps = Array(n);
// Pregenerate objects
for (let i = 0; i < n; i++) {
objects[i] = {};
objects[i]['k' + i] = null;
objects[i].x = i;
maps[i] = new Map();
maps[i].set('x', i);
maps[i].set('k' + i, null);
}
function object(x) {
return x.x + x.x;
}
function map(x) {
return x.get('x') + x.get('x');
}
suite
.add('Objects', function() {
for (var i = 0; i < n; i++) {
object(objects[i]);
}
})
.add('Maps', function() {
for (var i = 0; i < n; i++) {
map(maps[i]);
}
})
.on('error', error => {
console.error(error);
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run();
Objects x 46.53 ops/sec ±1.47% (55 runs sampled)
Maps x 126 ops/sec ±1.37% (67 runs sampled)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment