我刚开始使用benchmark.js,文档有点刺耳,找不到很多例子,请任何人确认我的代码是否正确,很抱歉我不能分享整个代码(公司政策)。
将setText(choice);视为某种操作,我想比较各种选择。(功能独立工作,我已经验证过了)。虽然我正在设置设置和解压函数,但我不确定设置是否正确,我希望它们在每次运行setText(choice);之前和之后运行。
使用console.log时,我发现它们每200次运行一次setText(choice);,我希望它们每次运行一次。
此外,在完成套件时,我如何检索每一项操作/秒。您可以在下面找到我的基准测试套件相关代码。
var suite = new Benchmark.Suite;
suite.add('innerText', function() {
setText('innerText');
}, {'setup':setup,'teardown':teardown})
.add('innerHTML', function() {
setText('innerHTML');
}, {'setup':setup,'teardown':teardown})
.add('textContent', function() {
setText('textContent');
}, {'setup':setup,'teardown':teardown})
.on('cycle', function(event, bench) {
log(String(bench));
}).on('complete', function() {
log('Fastest is ' + JSON.stringify(this));
}).run(false);发布于 2015-01-08 09:48:42
我也是新的基准,希望我能正确回答。
http://monsur.hossa.in/2012/12/11/benchmarkjs.html
如果您阅读上面的文章,您将发现基准将进入分析和抽样阶段。
它将运行一些迭代(即“测试循环”),在采样阶段执行setText,而不调用setup或teardown。
因此,我想设置和解压只是为了您在进入测试循环之前所做的事情,而不是每个测试的。您不能获得比测试循环更细的粒度。
您可以获得比测试循环更少的粒度;您可以在整个基准测试(vs在套件级或测试循环级别)上侦听事件。第二个问题是关于套件完成后的每个操作,您可以从完全事件中得到它。
.on('complete', function(event) {
//will log out the array of benchmark.suite, you can get it from each benchmark suite.
//e.g event.currentTarget[0].hz which retrieve the operation/s. for the first one.
console.log(event.currentTarget);
//equivalent of above; event.currentTarget===this; both are Benchmark.suite.
console.log(this);
})https://stackoverflow.com/questions/26775280
复制相似问题