I learned from books that you should write for loop like this:
for(var i=0, len=arr.length; i < len; i++){
// blah blah
}
so the arr.length
will not be calculated each time.
Others say that the compiler will do some optimization to this, so you can just write:
for(var i=0; i < arr.length; i++){
// blah blah
}
I just want to know which is the best way in practice?
This looks to be the fastest way by far...
Take into account that this will consume the array, eating it, and leaving nothing left...
I'm always write in the first style.
Even if a compiler is smart enough to optimize it for arrays, but still it smart if we are using DOMNodeList here or some complicated object with calculated length?
I know what the question is about arrays, but i think it is a good practice to write all your loops in one style.
2014
While
is backJust think logical.
Look at this
for
loop has 3 parametersNow tell me why this should be faster than:
while
has only one parameterI was totally confused when Chrome 28 showed that the for loop is faster than the while. This must have ben some sort of
"Uh, everyone is using the for loop, let's focus on that when developing for chrome."
But now, in 2014 the while loop is back on chrome. it's 2 times faster , on other/older browsers it was always faster.
Lately i made some new tests. Now in real world envoirement those short codes are worth nothing and jsperf can't actually execute properly the while loop, because it needs to recreate the array.length which also takes time.
you CAN'T get the actual speed of a while loop on jsperf.
you need to create your own custom function and check that with
window.performance.now()
And yeah... there is no way the while loop is simply faster.
For example i have a canvas scene where i need to calculate the coordinates and collisions... this is done between 10-200 MicroSeconds (not milliseconds). it actually takes various milliseconds to render everything.Same as in DOM.
BUT
There is another super performant way using the for
loop
in some cases... for example to copy/clone an arrayNotice the setup of the parameters:
Said that, this confirms that machines like the --
writing that i was thinking to make it a little shorter and remove some useless stuff and wrote this one using the same style:
Even if it's shorter it looks like using
i
one more time slows down everything. It's 1/5 slower than the previousfor
loop and thewhile
one.Note: the
;
is very important after the for looo without{}
Even if i just told you that jsperf is not the best way to test scripts .. i added this 2 loops here
http://jsperf.com/caching-array-length/40
And here is another answer about performance in javascript
https://stackoverflow.com/a/21353032/2450730
This answer is to show performant ways of writing javascript. So if you can't read that, ask and you will get an answer or read a book about javascript http://www.ecma-international.org/ecma-262/5.1/
As of September 2017 these jsperf tests are showing the following pattern to be most performant on Chrome 60:
Is anyone able to reproduce?
**cache the array length inside the loop ,some seconds of time will be eluded . Depends on the items in the array if there are more items in array there is major difference with respect to Ms of time*
**
**
**
**
It's the year 2017.
I made some tests.
https://jsperf.com/fastest-way-to-iterate-through-an-array/
Looks like the
while
method is the fastest on Chrome.Looks like the left decrement (
--i
) is much faster than the others (++i
,i--
,i++
) on Firefox.This approach is the fasted on average. But it iterates the array in reversed order.
If the forward order is important, use this approach.