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 or Adobe AIR:
<script src="benchmark.js"></script>
Optionally, expose Java’s nanosecond timer by adding the nano
applet to the <body>
:
<applet code="nano" archive="nano.jar"></applet>
Or enable Chrome’s microsecond timer by using the command line switch:
--enable-benchmarking
Via npm:
npm install benchmark
In Node.js:
var Benchmark = require('benchmark');
Optionally, use the microtime module by Wade Simmons:
npm install microtime
var Benchmark = require('benchmark').Benchmark;
In Rhino:
load('benchmark.js');
Usage example:
var suite = new Benchmark.Suite;
// add tests
suite.add('RegExp#test', function() {
/o/.test('Hello World!');
})
.add('String#indexOf', function() {
'Hello World!'.indexOf('o') > -1;
})
// add listeners
.on('cycle', function(bench) {
console.log(String(bench));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
// run async
.run(true);
// logs:
// > RegExp#test x 4,161,532 +-0.99% (59 cycles)
// > String#indexOf x 6,139,623 +-1.00% (131 cycles)
// > Fastest is 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
cd Benchmark.js
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!