for循环vs for jach javascript中的性能和jsperf结果的可信度
我不相信jsperf测量for循环与forEach性能的结果。至少对我机器上的chrome和firefox来说,结果与jsperf中广告的结果完全不同。
http://jsperf.com/foreach-vs-loop (我的)
http://jsben.ch/#/BQhED (更受欢迎)
在我的笔记本电脑上运行Ubuntu 11.10我在Firefox中有以下结果:
I don't trust results from jsperf measuring performance of for loop vs forEach. At least for chrome and firefox on my machine results are completely different than the ones being advertised in jsperf.
http://jsperf.com/foreach-vs-loop (mine)
http://jsben.ch/#/BQhED (more popular)
On my laptop running Ubuntu 11.10 I have the following results in Firefox:
for: total=1641 ms, avg=164.1 ms
forEach: total=339 ms, avg=33.9 ms
uname -a:
Linux 3.0.0-16-generic #29-Ubuntu SMP Tue Feb 14 12:48:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
不幸的是,Chrome不会返回console.timeEnd()的结果,但Chrome的运行时间相同而且速度更快。我观察到forEach比Chrome中的循环快10倍,而Firefox的速度快3倍。
在Chrome中,我得到大约这些运行时间:
Unfortunately, Chrome doesn't return the result of console.timeEnd() but the running times are same and just faster in Chrome. I'm observing that forEach almost 10x faster than for loop in Chrome, and 3x faster in Firefox.
In Chrome I'm getting approximately these running times:
for: avg=80 ms
forEach: avg=6 ms
这是我在Firefox和Chrome控制台中运行的代码。
Here's the code I ran in Firefox and Chrome console.
var arr = [];
for(var i = 0; i < 100000; i++) arr[i]=i;
var numberOfRuns = 10;
function time(name, f){
console.time(name);
f();
return console.timeEnd(name);
}
function runTest(name, f){
var totalTime = 0;
for(var r = 0; r < numberOfRuns; r++)
totalTime += time(name,f);
return totalTime;
}
var forTime = runTest('for', function(){
for(var j = 0; j < arr.length; j++)
arr[j];
});
var forEachTime = runTest('forEach', function(){
arr.forEach(function(v){v;});
});
console.log('for', {total:forTime, avg:forTime / numberOfRuns});
console.log('forEach', {total:forEachTime, avg:forEachTime / numberOfRuns});
运行一百万个项目的测试具有相同的性能差异。你能否告诉我,如果我错过了什么,我应该相信jsperf结果,而不是我正在观察的真实结果?当然,我确实相信我现在可以在浏览器中看到的真实结果。
Running the tests for one million items has the same performance difference. Could you please advise if I'm missing something and I should trust jsperf results instead of the real ones I'm observing? Of course I do trust the real results that I can see right here right now in my browser.
编辑:在与@讨论时发现的测试场景不客观搅拌机。看起来像js优化器optimezes forEach循环没有动作,因此如果有一些真正的代码就会模糊运行时间。
The test scenario isn't objective as discovered during discussion with @Blender. Looks like js optimizer optimezes forEach loop with no action in it and thus obscures running time if there were some real code.
我修改你的代码更公平。你能看一下吗?
I modified your code to be more fair. Can you take a look at it?
var arr = [];
for (var i = 0; i < 100000; i++) arr[i] = i;
var numberOfRuns = 100;
function runTest(name, f) {
var totalTime = 0;
console.time(name);
for (var r = 0; r < numberOfRuns; r++) {
f();
}
return console.timeEnd(name);
}
function testFunction(v) {
v;
}
var forTime = runTest('for', function() {
for (var j = 0; j < arr.length; j++) {
testFunction(arr[j]);
}
});
var forEachTime = runTest('forEach', function() {
arr.forEach(testFunction);
});
您的测试不是100%原始数字运算,因此某些浏览器对基准测试进行了不公平的优化。
Your test wasn't 100% raw number crunching, so the benchmark was being optimized unfairly by some browsers.