Open Bug 431909 Opened 12 years ago Updated 8 years ago

for each over Arrays still considered harmful

Categories

(Firefox :: General, defect)

defect
Not set

Tracking

()

People

(Reporter: jtolds, Unassigned)

References

Details

User-Agent:       Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9b4) Gecko/2008030318 Firefox/3.0b4
Build Identifier: Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9b4) Gecko/2008030318 Firefox/3.0b4

There are still a bunch of places where a for each is used over an array. This breaks when an extension modifies the array prototype (which is relatively common)

Examples include FillInHTMLTooltip in browser.js, among others.

Reproducible: Always




Please see bug 412600 (currently marked as closed) for a previous discussion.
Status: UNCONFIRMED → NEW
Ever confirmed: true
Besides the discussion in bug 412600, for each and for..in are also slower than plain for loops. I'm not sure about Array.forEach, but it's said to be quite fast.
Depends on: 524661
Depends on: 581654
FYI I did a search for forEach in mozilla central and found hundreds of matches, and many did not seem to be in test code, not sure if or how many of these are custom implementations & uses, on an object for instance.

Also did some testing http://jsperf.com/array-for-loops and forEach also appears to be about 1/4 to 1/5 as fast as a normal for loop.
(In reply to comment #2)
> FYI I did a search for forEach in mozilla central and found hundreds of
> matches, and many did not seem to be in test code, not sure if or how many of
> these are custom implementations & uses, on an object for instance.

forEach is fine, is "for each" that is unsafe if done on arrays.
(In reply to comment #3)
> (In reply to comment #2)
> > FYI I did a search for forEach in mozilla central and found hundreds of
> > matches, and many did not seem to be in test code, not sure if or how many of
> > these are custom implementations & uses, on an object for instance.
> 
> forEach is fine, is "for each" that is unsafe if done on arrays.

Ah perhaps I should make another issue, my mistake. My point is simply that forEach is much much slower than simply using a for loop.
(In reply to comment #4)
> perhaps I should make another issue

https://bugzilla.mozilla.org/show_bug.cgi?id=598771
You need to log in before you can comment on or make changes to this bug.