Created
November 24, 2018 11:39
-
-
Save pavel-surinin/4066dcb5a893fa064683d5bed539e7a1 to your computer and use it in GitHub Desktop.
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
// npm i benchmark@2.1.4 -D | |
var Benchmark = require('benchmark'); | |
var suite = new Benchmark.Suite; | |
// array of 200 elements | |
var { data } = require('../data') | |
class CalcAF { | |
constructor() { | |
this.add = (x, y) => { | |
return x + y | |
} | |
this.sub = (x, y) => { | |
return x - y | |
} | |
this.mul = (x, y) => { | |
return x * y | |
} | |
this.div = (x, y) => { | |
return x / y | |
} | |
this.pow = (x) => { | |
return Math.pow(x) | |
} | |
this.powpow = (x) => { | |
return Math.pow(x) * Math.pow() | |
} | |
} | |
addTwice(x, y) { | |
return this.add(x, y) * 2 | |
} | |
subTwice(x, y) { | |
return this.sub(x, y) * 2 | |
} | |
} | |
class CalcPR { | |
constructor() { | |
this.add.bind(this) | |
this.sub.bind(this) | |
this.mul.bind(this) | |
this.div.bind(this) | |
this.pow.bind(this) | |
this.powpow.bind(this) | |
} | |
mul(x, y) { | |
return x * y | |
} | |
div(x, y) { | |
return x / y | |
} | |
pow(x) { | |
return Math.pow(x) | |
} | |
powpow(x) { | |
return Math.pow(x) * Math.pow() | |
} | |
add(x, y) { | |
return x + y | |
} | |
sub(x, y) { | |
return x - y | |
} | |
addTwice(x, y) { | |
return this.add(x, y) * 2 | |
} | |
subTwice(x, y) { | |
return this.sub(x, y) * 2 | |
} | |
} | |
class CalcCall { | |
constructor() { | |
} | |
add(x, y) { | |
return x + y | |
} | |
sub(x, y) { | |
return x - y | |
} | |
addTwice(x, y) { | |
return this.add.call(this, x, y) * 2 | |
} | |
subTwice(x, y) { | |
return this.sub.call(this, x, y) * 2 | |
} | |
} | |
const calcAF = new CalcAF() | |
const calcPR = new CalcPR() | |
const calcC = new CalcCall() | |
suite | |
.add('Arrow function props (new)', function () { | |
new CalcAF() | |
}) | |
.add('Prototype props bind (new)', function () { | |
new CalcPR() | |
}) | |
.add('Prototype props call (new)', function () { | |
new CalcCall() | |
}) | |
.add('Arrow function props ', function () { | |
calcAF.addTwice(100, 100) | |
calcAF.subTwice(100, 100) | |
}) | |
.add('Prototype props bind ', function () { | |
calcPR.addTwice(100, 100) | |
calcPR.subTwice(100, 100) | |
}) | |
.add('Prototype props call ', function () { | |
calcC.addTwice(100, 100) | |
calcC.subTwice(100, 100) | |
}) | |
.add('group array of 200 items ', function () { | |
data.reduce( | |
function (acc, obj) { | |
var key = obj['from']; | |
if (!acc[key]) { | |
acc[key] = []; | |
} | |
acc[key].push(obj); | |
return acc; | |
}, {}) | |
}) | |
// 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({ id: 'benchmark', 'async': true, minSamples: 100000000 }); | |
// Arrow function props (new) x 682,691 ops/sec ±2.38% (80 runs sampled) | |
// Prototype props bind (new) x 7,791,488 ops/sec ±1.42% (80 runs sampled) | |
// Prototype props call (new) x 34,823,007 ops/sec ±1.27% (78 runs sampled) | |
// Arrow function props x 82,309,637 ops/sec ±13.22% (63 runs sampled) | |
// Prototype props bind x 84,506,412 ops/sec ±18.74% (64 runs sampled) | |
// Prototype props call x 103,412,085 ops/sec ±3.60% (76 runs sampled) | |
// group array of 200 items x 53,126 ops/sec ±2.78% (80 runs sampled) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment