Forked from benchmark.js
In a browser:
<script src="benchmark.js"></script>In an AMD loader:
require({
'paths': {
'benchmark': 'path/to/benchmark',
}
},
['benchmark'], function(Benchmark) {/*…*/});Using npm:
$ npm i --save benchmarkIn Node.js:
var Benchmark = require('benchmark');Optionally, use the microtime module by Wade Simmons:
npm i --save microtimevar Benchmark = require('benchmark');
function microtime() {
try {
const result = require('microtime');
console.log('using microtime');
return result;
} catch {
}
console.log('not using microtime');
return undefined;
}
const maybe_microtime = microtime();
if (maybe_microtime) {
Benchmark = Benchmark.runInContext(
undefined,
maybe_microtime,
);
}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) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('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#indexOfTested in Tested in Chromium (143.0.7499.4), Firefox (144.0.2), WebKit (26.0), Node (20-25)