Open Bug 1850272 Opened 10 months ago Updated 10 months ago

for-of loop considerably slower than for loop

Categories

(Core :: JavaScript Engine: JIT, defect, P3)

Firefox 117
x86_64
macOS
defect

Tracking

()

People

(Reporter: me, Unassigned)

References

(Blocks 2 open bugs)

Details

According to this benchmark, a for-of loop is 10-20 times slower than a plain for in Firefox for me, which seems like a bug because in other browsers, the difference is at most factor 2. I tested on macOS and Windows.

Comparing while making use of a result before calling console.log show that for-of is 19% of C-style for loop. To be more precise:

1000 elements: 19%
10000 elements: 14% (most likely ion compilation without the benefits)
100000 elements: 24%
1000000 elements: 27%

Thus, we are most likely slower in Baseline, and faster in Ion, but not enough to catch-up with the speed of the C-style for loop.

I do not see how other browser would achieve a factor of 2 between the 2 implementation unless either, they bake in from the bytecode the fact that iterators are likely to be manipulating arrays, and somehow avoid the complexity, or they reuse the compilation of functions with identical text.

The later option, of caching code based on text, is something we could do if we were interested in implementing a per-page caching mechanism which works across code updates on sites such as Facebook.

Severity: -- → S4
Component: JavaScript Engine → JavaScript Engine: JIT
Priority: -- → P3

Most of our for-of optimizations currently depend on inlining and scalar replacement, which only happen in Ion. We have sketched out some improvements to how we handle iterators that should make iterating over arrays faster, starting in baseline. I would expect those changes to close the gap here.

You need to log in before you can comment on or make changes to this bug.