Skip to content

Instantly share code, notes, and snippets.

@harryi3t
Created March 7, 2018 11:23
Show Gist options
  • Save harryi3t/ab0494ca15d36ce51bdcb1207b957c45 to your computer and use it in GitHub Desktop.
Save harryi3t/ab0494ca15d36ce51bdcb1207b957c45 to your computer and use it in GitHub Desktop.
Quickly benchmark js code
var Benchmark = require('benchmark'),
suite = new Benchmark.Suite;
var x = new Array(20).fill(0);
// add tests
suite.add('test 1', function() {
// your code here
})
suite.add('test 2', function() {
// your code here
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
var opsHigh = this.filter('fastest').map('hz'),
opsLow = this.filter('slowest').map('hz'),
fasterBy = (opsHigh-opsLow)/opsLow;
fasterBy >= 2 ? (fasterBy = ~~fasterBy + ' times') : (fasterBy = ~~(fasterBy*100) + ' %')
console.log(`Fastest is ${this.filter('fastest').map('name')} faster by ${fasterBy} with the lowest`);
})
.run(
// { 'async': true }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment