That was a great way to iterate over a non-sparse array such as an array of objects... Back in 2006 when this article was written.
In fact, this was an even better way to write that loop in 2006:
for( var value, i = -1; value = array[++i]; ) {
console.log( 'index: ' + i, 'value: ' + value );
}
Basically the same thing except for the pre-increment instead of postincrement. In some of the browsers of that day (at least IE), preincrement was faster. And this version gives you the correct array index inside the loop, unlike the off-by-one in the other version.
I was so proud of myself when I discovered this alternative that I wrote a recipe for the jQuery Cookbook recommending it as the fastest way to iterate over a typical array of objects such as you might receive from a JSON request.
It also used to be a great idea to optimize your machine code programs by looking up the CPU cycles for each instruction. Fewer cycles = faster code.
Of course, that doesn't work any more, what with pipelining and out-of-order execution.
Alas, these nifty JavaScript loops are not the fastest any more either. Fewer operations != faster code.
The optimization guy for V8 explained this to me at Google I/O 2012. He said that V8 can optimize the heck out of references into an array where all the types are the same (either primitive values, or objects that all have the same properties). But if you ever try to reference outside the bounds of the array, it throws the whole thing back into deoptimized code.
And that is the problem. These nifty loops rely on reading the nonexistent element just past the end of the array. That's their termination condition.
It turns out that with modern optimizing compilers, the plain old traditional JavaScript loop is faster across the board:
for( var i = 0, n = array.length; i < n; ++i ) {
var value = array[i];
console.log( 'index: ' + i, 'value: ' + value );
}
(There is still a minor benefit to using preincrement instead of the more traditional postincrement in some modern browsers.)
I put together a JSPerf for this a while ago:
http://jsperf.com/mikes-loops/2
I was quite embarrassed to learn how poorly my clever loop works in modern browsers!