A robust benchmarking library that works on nearly all JavaScript platforms, supports high-resolution timers, and returns statistically significant results. As seen on jsPerf.
The documentation for Benchmark.js can be viewed here: http://benchmarkjs.com/docs
For a list of upcoming features, check out our roadmap.
In a browser:
<script src="benchmark.js"></script>
Optionally, add the nanoTime
Java applet to the <body>
:
<applet code="nano" archive="nano.jar"></applet>
Via npm:
npm install benchmark
In Node.js:
var Benchmark = require('benchmark');
var Benchmark = require('benchmark').Benchmark;
In Rhino:
load('benchmark.js');
Usage example:
function testA() {
/o/.test('Hello World!');
}
function testB() {
'Hello World!'.indexOf('o') > -1;
}
var benches = [
new Benchmark('RegExp#test', testA),
new Benchmark('String#indexOf', testB)
];
// add listeners
Benchmark.invoke(benches, 'on', 'complete', function() {
console.log( this.toString() );
});
// run benchmarks (async)
Benchmark.invoke(benches, {
'name': 'run',
'args': true,
'onComplete': function() {
var a = benches[0],
b = benches[1],
rank = a.compare(b);
// report results
console.log(
a.name + ' is ' +
(rank == 0 ? ' <indeterminate> ' : rank > 0 ? 'faster' : 'slower') +
' than ' + b.name
);
}
});
// logs:
// > RegExp#test × 4,161,532 ±0.99% (59 cycles)
// > String#indexOf × 6,139,623 ±1.00% (131 cycles)
// > RegExp#test is slower than String#indexOf
To clone this repository including all submodules, using git 1.6.5 or later:
git clone --recursive https://github.com/mathiasbynens/benchmark.js.git
For older git versions, just use:
git clone https://github.com/mathiasbynens/benchmark.js.git
cd benchmark.js
git submodule update --init
Feel free to fork if you see possible improvements!