JavaScript Loop Performance
jsperf.com
jsperf.com
Testing in Chrome 27.0.1453.116 32-bit on Windows Server 2008 R2 / 7 64-bitvar i = items.length; while(i--) { total += items[i]; }
and
var i = items.length - 1; do { total += items[i] } while(i--);
I think those would compare differently to the for loop.
Turns out I was wrong and I made the for loop faster: http://jsperf.com/optimal-loops
But I ran it in firefox and got completely different results, namely the for loop handling hundreds of millions of items per second, and the loops with .shift() being handled literally and going at the speed of sludge (it reports 0.25 items per second).
But, atm, functional operations on lists are still executed single-threaded. Wouldn't mind seeing a WebWorker implementation, for example. Although somehow I think the overhead for a relatively simple calculation like this would negate the results.
The amount of work done in the test case is very small (actually dwarfed by the setup part of the test), making it subject to huge variance due to factors like task switching, GC pauses, etc.
The result of the benchmark is also never used so it is possible for a valid JS runtime to partially or completely optimize it out.
Benchmark actual code doing actual work. Code reduced to this level of meaninglessness does not allow you to draw meaningful conclusions about actual JS performance.
These are the sort of results that I'd think would be good to keep in the back of your mind, and little else.
I once went to a meetup presentation where the presenter was a supposed JS performance expert. His presentation was based on JS perf results, and Chrome was consistently showing 749.8M ops per second for complex side-effect free js code.
The only thing I learned from the presentation was that Chrome is good optimizing out code that does nothing, and the presenter's laptop ran at 2.25 Ghz.
However the following examples all do nothing (except increasing i) and get different results.
http://jsperf.com/codethatdoesnothing/2 http://jsperf.com/functionwithwhilethatdoesnothing
JS Perf is fundamentally broken. It doesn't force a side effect, and from the debugger, the first step it does is something like:
while(100,000,000--) {
// your code here
}
The chrome compiler then recognizes the loops can be swapped, and eliminated, thus it runs your code once, yet jsperf thinks it ran it hundreds of millions of times.Two of the test cases destructively modify the list, while the first does not. Of course they will be slower.
For others trying to understand this comment: the TEST loop which runs the code being benchmarked is short-circuited in the 'do' and 'while' cases because the setup code does not get re-run, so on the first test loop, it sums up an array of random numbers, and on subsequent runs, it sums an empty array. This also gave me the 'aha' that explained why the huge difference between 'while' and 'do': in the 'do' case, we execute the loop at least once in every one of the hundreds of millions of test runs, whereas in the 'while' case, the loop is executed 0 times in all but one run.