Created
March 7, 2018 11:23
-
-
Save harryi3t/ab0494ca15d36ce51bdcb1207b957c45 to your computer and use it in GitHub Desktop.
Quickly benchmark js code
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
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