If you want to have a fast responsive code then:
for arrays the while( l--)
and the for(i=0,..)
loops are the fastest one...
in object arrays you can only use for in... but if your code is good it does no happen very often that you loop through an object.
fastest loops:
var myarray=[1,2,3,4,5,6],
length=myarray.length;//here i cache the length of the array.
caching the array length is another important thing to keep the code fast.
while(length--){
//do somethingwith your array
//myarray[length];
}
while was the fastest loop in older browsers .. in new browsers it looks like the for loop is a little faster.
for(var i=0;i<length;i++){
//do somethingwith your array
//myarray[i];
}
now if u wanna define variables, do that before you loop.
there is also a nice comparsion that shows you the performance even if sometimes the code is not written very well.
http://jsperf.com/fors-vs-while/61
so if you plan to loop through an array always use while-- or for(var i=0..) caching the length.
another loop that i really like if u have arrays in multidimensional objects like json is this one
for(var a=0,b;b=my.very.long.object[a];++a){
//do somethingwith your array
//b
}
another mistake that most ppl do is they use push in a loop...
executing push in a loop means u executea new function every time.
as we already have "i" that is a index we can set that and use it to store the data directly without executing 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 free to correct my bad english but don't touch the code!.