Benchmark.js v1.0.0

build status

A robust benchmarking library that works on nearly all JavaScript platforms1, supports high-resolution timers, and returns statistically significant results. As seen on jsPerf.

Download

Dive in

We’ve got API docs and unit tests.

For a list of upcoming features, check out our roadmap.

Support

Benchmark.js has been tested in at least Adobe AIR 3.1, Chrome 5-21, Firefox 1-15, IE 6-9, Opera 9.25-12, Safari 3-6, Node.js 0.8.8, Narwhal 0.3.2, RingoJS 0.8, and Rhino 1.7RC5.

Installation and usage

In a browser or Adobe AIR:

  1. <script src="benchmark.js"></script>

Optionally, expose Java’s nanosecond timer by adding the nano applet to the <body>:

  1. <applet code="nano" archive="nano.jar"></applet>

Or enable Chrome’s microsecond timer by using the command line switch:

  1. --enable-benchmarking

Via npm:

  1. npm install benchmark

In Node.js and RingoJS v0.8.0+:

  1. var Benchmark = require('benchmark');

Optionally, use the microtime module by Wade Simmons:

  1. npm install microtime

In RingoJS v0.7.0-:

  1. var Benchmark = require('benchmark').Benchmark;

In Rhino:

  1. load('benchmark.js');

In an AMD loader like RequireJS:

  1. require({
  2. 'paths': {
  3. 'benchmark': 'path/to/benchmark'
  4. }
  5. },
  6. ['benchmark'], function(Benchmark) {
  7. console.log(Benchmark.version);
  8. });
  9. // or with platform.js
  10. // https://github.com/bestiejs/platform.js
  11. require({
  12. 'paths': {
  13. 'benchmark': 'path/to/benchmark',
  14. 'platform': 'path/to/platform'
  15. }
  16. },
  17. ['benchmark', 'platform'], function(Benchmark, platform) {
  18. Benchmark.platform = platform;
  19. console.log(Benchmark.platform.name);
  20. });

Usage example:

  1. var suite = new Benchmark.Suite;
  2. // add tests
  3. suite.add('RegExp#test', function() {
  4. /o/.test('Hello World!');
  5. })
  6. .add('String#indexOf', function() {
  7. 'Hello World!'.indexOf('o') > -1;
  8. })
  9. // add listeners
  10. .on('cycle', function(event) {
  11. console.log(String(event.target));
  12. })
  13. .on('complete', function() {
  14. console.log('Fastest is ' + this.filter('fastest').pluck('name'));
  15. })
  16. // run async
  17. .run({ 'async': true });
  18. // logs:
  19. // > RegExp#test x 4,161,532 +-0.99% (59 cycles)
  20. // > String#indexOf x 6,139,623 +-1.00% (131 cycles)
  21. // > Fastest is String#indexOf

BestieJS

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.

Authors

Contributors