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('String#match', function() {
!!'Hello World!'.match(/o/);
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': true });
Benchmark.js’ only hard dependency is lodash. Include platform.js to populate Benchmark.platform.
In a browser:
<script src="lodash.js"></script>
<script src="platform.js"></script>
<script src="benchmark.js"></script>
Using npm:
$ {sudo -H} npm i -g npm
$ npm i --save benchmark
In Node.js:
var Benchmark = require('benchmark');
Optionally, use the microtime module for Node.js by Wade Simmons:
$ npm i --save microtime
Tested in Chrome 46-47, Firefox 42-43, IE 9-11, Edge 13, Safari 8-9, Node.js 0.10.x, 0.12.x, 4.x, & 5.x, & PhantomJS 1.9.8.