安装:npm install benchmark -g
// 引入 benchmark
const benchmark = require('benchmark');
const suite = new benchmark.Suite;
// 添加 RegExp 方法
suite.add('RegExp', function () {
/0/.test('Hello World');
})
// 添加 indexof 方法
.add('indexof', function () {
'Hello World'.indexOf('o');
})
// 完成单个处理方法的回调
.on('cycle', function (event) {
console.log(String(event.target))
})
// 完成所有方法的回调
.on('complete', function () {
// 判断那个方法最快
console.log('the fasted method is ' + this.filter('fastest').map('name'))
})
// 开始运行
.run({ 'async': true })