nodejs 性能测试 benchmark

test.js

const benchmark = require('benchmark');
const 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})

output

RegExp x 26,365,573 ops/sec ±3.08% (70 runs sampled)
indexOf x 786,281,744 ops/sec ±2.26% (78 runs sampled)
The fasted method is indexOf