Benchmark.js: how to display / read results (ops / sec)?
I can successfully create and run the test suite, but did not know how to get reference values for each output this.filter('fastest').pluck('name')
to onComplete
give me the name of the fastest operation, but I want ops/sec
the value of each function in the set of tests. how to get it?
source to share
In your callback, onComplete
you can access your criteria with a keyword this
(which will reference the current BenchmarkSuit object ) like this
var bench1 = this[0];
var bench2 = this[1];
...
var benchN = this[N-1];
Eeach bench
is a Benchmark instance . This way you can get any information you need (see Benchmark.prototype ). To get the value ops/sec
use the property of the .hz
test property . A small example for better understanding:
new Benchmark.Suite()
.add('test1', function() {
// some code
})
.add('test2', function() {
// some code
})
.on('complete', function() {
var benchTest1 = this[0]; // gets benchmark for test1
var benchTest2 = this[1]; // gets benchmark for test2
console.log(benchTest1.hz); // ops/sec
// benchmark info in format:
// test2 x 1,706,681 ops/sec Âą1.18% (92 runs sampled)
console.log(benchTest2.toString());
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
.run();
source to share