If you want to have responsive code, then:
for while( l--) arrays and for(i=0,..) loops for(i=0,..) are the fastest ...
in arrays of objects that you can only use for ... but if your code is good, it doesn’t often happen that you go through the object.
fastest cycles:
var myarray=[1,2,3,4,5,6], length=myarray.length;
Array length caching is another important thing to save code quickly.
while(length--){
while it was the fastest loop in old browsers .. in new browsers, it looks like a for loop works a little faster.
for(var i=0;i<length;i++){
now, if you want to define variables, do it before the loop.
There is also a nice simplex that shows you performance, even if sometimes the code is not written very well.
http://jsperf.com/fors-vs-while/61
therefore, if you plan to loop through an array, always use while - or for (var i = 0 ..) length caching.
another loop that I really like if you have arrays in multidimensional objects like json is
for(var a=0,b;b=my.very.long.object[a];++a){
another mistake most ppl makes is to use push in a loop ... doing push in a loop means u executes a new function every time. since we already have an “i”, which is an index, we can set it and use it to store data directly without performing a new function
So
//wrong for(var i=0,newArray=[];i<length;i++){ newArray.push(myarray[i]);//this is a waste of time and resources } //right for(var i=0,newArray=[];i<length;i++){ newArray[i]=myarray[i];//we already have an index. }
ps.:feel to fix my bad english, but don't touch the code!