Created
June 21, 2016 12:13
-
-
Save rumkin/f7542ead1dc754309811fe0ca2818183 to your computer and use it in GitHub Desktop.
Compare objects with variadic property set versus Map
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
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(); |
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
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