A robust benchmarking library that works on nearly all JavaScript platforms1, supports high-resolution timers, and returns statistically significant results. As seen on jsPerf.
Benchmark.js is part of the BestieJS "Best in Class" module collection. This means we promote solid browser/environment support, ES5 precedents, unit testing, and plenty of documentation.
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');
In RequireJS:
require(['path/to/benchmark'], function(Benchmark) {
console.log(Benchmark.version);
});
// auto-require platform.js
// https://github.com/bestiejs/platform.js
require({
'paths': { 'platform': 'path/to/platform' }
},
['path/to/benchmark'], function(Benchmark) {
console.log(Benchmark.platform.name);
});
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(event, bench) {
console.log(String(bench));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
// run async
.run({ 'async': 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/bestiejs/benchmark.js.git
cd benchmark.js
For older git versions, just use:
git clone https://github.com/bestiejs/benchmark.js.git
cd benchmark.js
git submodule update --init
Feel free to fork if you see possible improvements!
- Benchmark.js has been tested in at least Adobe AIR 2.6, Chrome 4/8/13, Firefox 2-4, IE 6-10, Opera 9.25-11, Safari 2-5, Node.js 0.4.1, Narwhal 0.3.2, Ringo 0.6, and Rhino 1.7RC2. ↩