server.js
var benchmark = require('benchmark');
var suite = new benchmark.Suite;
// 添加测试
suite.add('RegExp', function() {
/o/.test('Hello World');
}).add('indexOf', function() {
'Hello World'.indexOf('o');
}).on('cycle', function(e) {
console.log(String(e.target))
}).on('complete', function() {
console.log('The fasted method is ' + this.filter('fastest').map('name'));
}).run({'async': true})
// 运行结果
RegExp x 29,720,354 ops/sec ±2.36% (83 runs sampled)
indexOf x 784,401,225 ops/sec ±2.17% (88 runs sampled)
The fasted method is indexOf