javascriptbenchmark.js

How to display/read results (ops/sec)?


I am able to successfully create and run benchmark suite, but not sure how to get the benchmark values of each output, this.filter('fastest').pluck('name') in onComplete gives me the name of the fastest operation, but I want the ops/sec value of each function in the test suite. how to get that?


Solution

  • In your onComplete callback you can access your benchmarks through this keyword (which will reference to the current BenchmarkSuite object) like this:

    var bench1 = this[0];
    var bench2 = this[1];
    ...
    var benchN = this[N-1];
    

    Each bench is instance of Benchmark. So you can get any information you want (see Benchmark.prototype ). To get ops/sec value use .hz property of benchmark. 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();