for-of loop considerably slower than for loop
Categories
(Core :: JavaScript Engine: JIT, defect, P3)
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.
Comment 1•1 year ago
|
||
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.
Comment 2•1 year ago
|
||
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.
Updated•5 months ago
|
Updated•5 months ago
|
Description
•