Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
'use strict'
// const v8 = require('v8-natives')
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 MyModel extends Base {}
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))))) {}
const n = 100000
suite.add('Just Base', function () {
let bar = 0
for (let i = 0; i < n; i++)
bar = bar + (new Base().foo())
return bar
}).add('0 mixin (extend Base)', function () {
let bar = 0
for (let i = 0; i < n; i++)
bar = bar + (new MyModel().foo())
return bar
}).add('1 mixin', function () {
let bar = 0
for (let i = 0; i < n; i++)
bar = bar + (new MyModelOne().foo())
return bar
}).add('2 mixin', function () {
let bar = 0
for (let i = 0; i < n; i++)
bar = bar + (new MyModelTwo().foo())
return bar
}).add('3 mixin', function () {
let bar = 0
for (let i = 0; i < n; i++)
bar = bar + (new MyModelThree().foo())
return bar
}).add('4 mixin', function () {
let bar = 0
for (let i = 0; i < n; i++)
bar = bar + (new MyModelFour().foo())
return bar
}).add('5 mixin', function () {
let bar = 0
for (let i = 0; i < n; i++)
bar = bar + (new MyModelFive().foo())
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
You can’t perform that action at this time.