To be honest I wrote something a bit redundant, this one is better if you
want to use the length.
Array.forEach = Array.forEach || function(obj, callback, scope){
for(var i = 0; i < obj.length; ++i){
if(i in obj)
callback.call(scope, obj[i], i, obj);
};
};
even more simple, isn't it?
The scenario with DOM is tipically this one
div
div
div
div
div
getElementsByTagName("div") will have length 5, let's say the first one, the
outer div, match something we were looking for and we remove it. With above
version of Array.forEach the loop will be instantly stopped at next i <
obj.length, because it changed, while with cached length, not updated, the
loop will go on other 4 times and the i in obj will always fail. I guess
then this version is, generally speacking, faster.
I am going to investigate a bit more about FireFox native behaviour though.
Regards
2009/5/22 Andrea Giammarchi <[email protected]>
> Actually, the main usage of Array.forEach is with non Array, like live
> objects. Since with DOM is easy to remove a node and the result of the live
> object could be completely changed, do you prefere 100 useless if I in obj
> or just a loop brek thanks to changed length? Dunno which is faster and the
> performance problem is the if, necessary if you want specs like, not the
> length. Regards
>
> On May 22, 2009 1:05 AM, "Robert Katić" <[email protected]> wrote:
>
>
> There is no need to take in consideration eventual length updates;
> that slows considerably, and it is not garanted in any/each native
> implementation:
>
>
> https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Array/ForEach
>
> On May 21, 5:39 pm, Andrea Giammarchi <[email protected]>
> wrote:
>
> > IE8? obviously no, how can you pretend a browser that implemented >
> defineProperty only for Window...
>
> > On Thu, May 21, 2009 at 4:11 PM, diogobaeder <[email protected]>
> wrote: > > > Hmmm... close, I...
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---