Are loops really faster in reverse?
This guy compared a lot of loops in javascript, in a lot of browsers. He also has a test suite so you can run them yourself.
In all cases (unless I missed one in my read) the fastest loop was:
var i = arr.length; //or 10
while(i--)
{
//...
}
It's not that i--
is faster than i++
. Actually, they're both equally fast.
What takes time in ascending loops is evaluating, for each i
, the size of your array. In this loop:
for(var i = array.length; i--;)
You evaluate .length
only once, when you declare i
, whereas for this loop
for(var i = 1; i <= array.length; i++)
you evaluate .length
each time you increment i
, when you check if i <= array.length
.
In most cases you shouldn't even worry about this kind of optimization.