Skip to content

Instantly share code, notes, and snippets.

@fl0w
Last active March 21, 2017 13:58
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 fl0w/0b16b4a31d3f8809d5e5351832ba0196 to your computer and use it in GitHub Desktop.
Save fl0w/0b16b4a31d3f8809d5e5351832ba0196 to your computer and use it in GitHub Desktop.
trying to catch performance impact of mixins in node
'use strict'
const v8 = require('v8-natives')
// requires node v6+ as --allow-natives-syntax is changed
const Benchmark = require('benchmark')
const suite = new Benchmark.Suite()
const MixinOne = (Base) => class extends Base {}
const MixinTwo = (Base) => class extends Base {}
const MixinThree = (Base) => class extends Base {}
const MixinFour = (Base) => class extends Base {}
const MixinFive = (Base) => class extends Base {}
class Base {
foo () {
return Math.random()
}
}
class MyModelOne extends MixinOne(Base) {}
class MyModelTwo extends MixinTwo(MixinOne(Base)) {}
class MyModelThree extends MixinThree(MixinTwo(MixinOne(Base))) {}
class MyModelFour extends MixinFour(MixinThree(MixinTwo(MixinOne(Base)))) {}
class MyModelFive extends MixinFive(MixinFour(MixinThree(MixinTwo(MixinOne(Base))))) {}
v8.neverOptimizeFunction(Base)
v8.neverOptimizeFunction(MyModelOne)
v8.neverOptimizeFunction(MyModelTwo)
v8.neverOptimizeFunction(MyModelThree)
v8.neverOptimizeFunction(MyModelFour)
v8.neverOptimizeFunction(MixinOne)
v8.neverOptimizeFunction(MixinTwo)
v8.neverOptimizeFunction(MixinThree)
v8.neverOptimizeFunction(MixinFour)
v8.neverOptimizeFunction(constructOne)
v8.neverOptimizeFunction(constructTwo)
v8.neverOptimizeFunction(constructThree)
v8.neverOptimizeFunction(constructFour)
v8.neverOptimizeFunction(constructFive)
function constructOne () {
return (new MyModelOne().foo())
}
function constructTwo () {
return (new MyModelTwo().foo())
}
function constructThree () {
return (new MyModelThree().foo())
}
function constructFour () {
return (new MyModelFour().foo())
}
function constructFive () {
return (new MyModelFive().foo())
}
const n = 100000
suite.add('1 mixin', function () {
let bar = 0
for (let i; i < n; i++)
bar = bar + constructOne()
return bar
}).add('2 mixin', function () {
let bar = 0
for (let i; i < n; i++)
bar = bar + constructTwo()
return bar
}).add('3 mixin', function () {
let bar = 0
for (let i; i < n; i++)
bar = bar + constructThree()
return bar
}).add('4 mixin', function () {
let bar = 0
for (let i; i < n; i++)
bar = bar + constructFour()
return bar
}).add('5 mixin', function () {
let bar = 0
for (let i; i < n; i++)
bar = bar + constructFive()
return bar
}).on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
}).on('cycle', function(event) {
console.log(String(event.target));
}).run({ 'async': true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment