Benchmark.js

A benchmarking library. As used on jsPerf.com.

README

Benchmark.js v2.1.4


A robust benchmarking library that supports high-resolution timers & returns statistically significant results. As seen on jsPerf.

Documentation



Download



Installation


Benchmark.js’ only hard dependency is lodash.
Include platform.js to populate Benchmark.platform.

In a browser:

  1. ``` html
  2. <script src="lodash.js"></script>
  3. <script src="platform.js"></script>
  4. <script src="benchmark.js"></script>
  5. ```

In an AMD loader:

  1. ``` js
  2. require({
  3.   'paths': {
  4.     'benchmark': 'path/to/benchmark',
  5.     'lodash': 'path/to/lodash',
  6.     'platform': 'path/to/platform'
  7.   }
  8. },
  9. ['benchmark'], function(Benchmark) {/*…*/});
  10. ```

Using npm:

  1. ``` sh
  2. $ npm i --save benchmark
  3. ```

In Node.js:

  1. ``` js
  2. var Benchmark = require('benchmark');
  3. ```

Optionally, use the microtime module by Wade Simmons:

  1. ``` sh
  2. npm i --save microtime
  3. ```

Usage example:

  1. ``` js
  2. var suite = new Benchmark.Suite;

  3. // add tests
  4. suite.add('RegExp#test', function() {
  5.   /o/.test('Hello World!');
  6. })
  7. .add('String#indexOf', function() {
  8.   'Hello World!'.indexOf('o') > -1;
  9. })
  10. // add listeners
  11. .on('cycle', function(event) {
  12.   console.log(String(event.target));
  13. })
  14. .on('complete', function() {
  15.   console.log('Fastest is ' + this.filter('fastest').map('name'));
  16. })
  17. // run async
  18. .run({ 'async': true });

  19. // logs:
  20. // => RegExp#test x 4,161,532 +-0.99% (59 cycles)
  21. // => String#indexOf x 6,139,623 +-1.00% (131 cycles)
  22. // => Fastest is String#indexOf
  23. ```

Support


Tested in Chrome 54-55, Firefox 49-50, IE 11, Edge 14, Safari 9-10, Node.js 6-7, & PhantomJS 2.1.1.

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, & plenty of documentation.