How to make the for loop run fast.
for(var i =0; i<myarray.length;i++)
{
var nice = myarray[i];
}
How to make the for loop run fast.
for(var i =0; i<myarray.length;i++)
{
var nice = myarray[i];
}
The best practice is to initiate the max value:
for(var i =0; max=myarray.length;i<max;i++)
{
// codes here
}
the reason is the length of the array is accessed on every loop iteration. This can slow down your code, especially when myarray is not an array but an HTMLCollection object.
the mirco -optimization way is to use count down:
var i,myarray=[];
for(i=myarray.length;i--;){
}
use one less variable(no max)
Count down to 0, which is usually faster because it is more efficient to compare to 0 than the length of the array or to anything other than 0.