The optimized approach is to cache the length of array and using the single varvariable pattern, initializing all variables with a single varvar
keyword.
var i, max, myStringArray = ["Hello", "World"];
for (i = 0, max = myStringArray.length; i < max; i++) {
alert(myStringArray[i]);
// Do something
}
If the order of iteration does not matter thanthen you should try reversed loop, it. It is the fastest as it reducereduces overhead condition testing and decrement is in one statement:
var i,myStringArray = ["item1","item2"];
for (i = myStringArray.length; i--) {
alert(myStringArray[i]);
}
orOr better and cleaner to use whilea while loop:
var myStringArray = ["item1","item2"],i = myStringArray.length;
while(i--) {
// doDo something with fruits[i]
}