The for statement is faster for sure. Even if the browser has native
forEach, the for will be faster.
But you can have a faster for like this:

for(var i=arr.length; i--;){
  arr[i];
}

As you can see it will loop the array on reverse order, if this is not a
problem use it. If the loop is considerably big and you need crescent order
you can cache the length property too so you dont have to look for it at
each iterations like this:

for(var i=0, l=arr.length; i<l ; i++){
  arr[i];
}

have a nice loop.


--
Fábio Miranda Costa
Solucione Sistemas
Engenheiro de interfaces
Twitter: fabiomiranda


On Thu, Apr 22, 2010 at 3:19 PM, Trevor Orr <[email protected]> wrote:

> I am working on a projects where there is a TON of array looping and
> performance is a priority.
>
> My question is what is going to be faster
>
> for(var x=0; x<somevar.length; x++) {
> myfunc(somevar[x]);
> }
>
> or
>
> somevar.each(function(item) {
> myfunc(item);
> });
>
>
>


-- 
Subscription settings: 
http://groups.google.com/group/mootools-users/subscribe?hl=en

Reply via email to