Skip to content

Instantly share code, notes, and snippets.

@rumkin

rumkin/result.txt

Last active Jun 21, 2016
Embed
What would you like to do?
Monomorphic vs megamorphic vs typed modes benchmark
Monomorphic x 1,989 ops/sec ±1.90% (71 runs sampled)
Megamorphic x 47.27 ops/sec ±1.03% (58 runs sampled)
Typed x 2,550 ops/sec ±2.10% (57 runs sampled)
const Benchmark = require('benchmark');
const suite = new Benchmark.Suite;
// Test data set size
const n = process.env.SET_SIZE || 100000;
// Monomorhpic objects array
const mono = Array(n);
// Megamorphic objects array
const mega = Array(n);
// Typed objects array
const typed = Array(n);
function Type(i) {
this.x =i;
this.n = null;
}
// Pregenerate objects
for (let i = 0; i < n; i++) {
mono[i] = {
n: null, // Add n to balance memory usage
x: i,
};
mega[i] = {};
mega[i]['k' + i] = null;
mega[i].x = i;
typed[i] = new Type(i);
}
function monoform(x) {
return x.x + x.x;
}
function megaform(x) {
return x.x + x.x;
}
function typedform(x) {
return x.x + x.x;
}
suite
.add('Monomorphic', function() {
for (var i = 0; i < n; i++) {
monoform(mono[i]);
}
})
.add('Megamorphic', function() {
for (var i = 0; i < n; i++) {
megaform(mega[i]);
}
})
.add('Typed', function() {
for (var i = 0; i < n; i++) {
typedform(typed[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 async
.run({async: false});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment