NEW283118
for-of loops noticeably slower than alternatives, including map
https://bugs.webkit.org/show_bug.cgi?id=283118
Summary for-of loops noticeably slower than alternatives, including map
Anthony Ricaud
Reported 2024-11-14 09:07:29 PST
Created attachment 473227 [details] Results in Safari, Firefox and Chrome On the linked benchmark, a simple for-of loop performs noticeably worse than a forEach loop or a classic for loop. It even performs worse than the equivalent map transformation which I find surprising because map has to do extra work (destructure the array, create a new one and replace it in the existing array). Firefox and Chrome results are roughly what I would expect: map is the slowest, classic loop is the fastest, forEach and for-of are in between.
Attachments
Results in Safari, Firefox and Chrome (811.92 KB, image/png)
2024-11-14 09:07 PST, Anthony Ricaud
no flags
Keith Miller
Comment 1 2024-11-14 13:55:22 PST
Hi thanks for the report. I just tried running ``` let dataPoints = [] for (let i=0; i <= 200_000; i++) { dataPoints[i] = [1, 2] } let start = performance.now(); function test() { for (const dataPoint of dataPoints) { dataPoint[1] *= 8 } // for (let i = 0; i < dataPoints.length; i++) { // dataPoints[i][1] *= 8 // } } test(); let end = performance.now(); print(end - start); ``` 100 times. Then commenting out the top and uncommenting the bottom and running another 100 times. I got an average of 4.817399999999999 for the first set and 4.576200000000002 for the second set. This seems like a 5% difference. Maybe something about how jsperf.app works is impacting the results?
Anthony Ricaud
Comment 2 2024-11-14 14:57:03 PST
How can I run that script locally? And yes, this could be because of the benchmarking does its work. - https://jsben.ch/bU9pT gives the kind of results I was expecting - https://jsbench.me/9rm3hwmqsn/1 seems to exhibit a similar issue as jsperf.app
Keith Miller
Comment 3 2024-11-14 15:58:59 PST
(In reply to Anthony Ricaud from comment #2) > How can I run that script locally? > > And yes, this could be because of the benchmarking does its work. > - https://jsben.ch/bU9pT gives the kind of results I was expecting > - https://jsbench.me/9rm3hwmqsn/1 seems to exhibit a similar issue as > jsperf.app It seems like you're on a Mac so you can do copy into a file then do: `/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc file.js`
Anthony Ricaud
Comment 4 2024-11-14 16:03:49 PST
Thanks. I'm getting this error though: Exception: ReferenceError: Can't find variable: performance
Keith Miller
Comment 5 2024-11-14 16:06:29 PST
(In reply to Anthony Ricaud from comment #4) > Thanks. > > I'm getting this error though: > Exception: ReferenceError: Can't find variable: performance Ah yeah, I ran on ToT. `performance` was added to the CLI somewhat recently, you could just use `Date.now()` although it's less accurate.
Anthony Ricaud
Comment 6 2024-11-14 16:16:50 PST
I have STP installed, can I access its JSC CLI ?
Keith Miller
Comment 7 2024-11-15 07:55:17 PST
(In reply to Anthony Ricaud from comment #6) > I have STP installed, can I access its JSC CLI ? Uhh, probably but I don't have one installed right now. I would just download a "nightly" from nightly.webkit.org and run the jsc in there. e.g. `DYLD_FRAMEWORK_PATH=286636@main/Release ./286636@main/Release/jsc`
Radar WebKit Bug Importer
Comment 8 2024-11-21 09:08:14 PST
Note You need to log in before you can comment on or make changes to this bug.