noForEach shouldn't be default on #4610
Replies: 4 comments
-
I think the documentation shouldn't talk about performance for two reasons:
If we removed the "speed" part from the documentation, would you be willing to keep it as recommended? |
Beta Was this translation helpful? Give feedback.
-
I'd agree that this rule should not be in the recommended set, at least in its current form. In addition to the questionable performance claims addressed above, the justification for the rule cites the improved readability of const myMap = new Map();
// calls to myMap.set()
myMap.forEach((item) => item.method());
for (const [_key, item] of myMap) {
item.method()
} Judging the readability of equivalent syntax is subjective, but I'd argue the TLDR: The rule's claim that |
Beta Was this translation helpful? Give feedback.
-
I disagree with the proposal here. I think I.e.
Reads OK until it gets longer, and then you get an exception on that line, and you do not know where it happened because you crammed too much code into 1 line. Doing a forEach at the end of a long chain, i.e. Using a proper for-loop forces people to break their code up so that it's readable and debuggable. Lastly, I'm tired of reviewing code from someone who loves forEach and write stuff like this:
and before you say this never happens, I've caught this error twice in last couple of years, and this is from devs who make $400k a year. So I think forEach should continue to be banned by default. |
Beta Was this translation helpful? Give feedback.
-
This rule has been removed from the recommended set, the change will be available in v2 |
Beta Was this translation helpful? Give feedback.
-
Hi,
noForEach
mentions this:While the latter makes sense, the former doesn't. I've benchmarked forEach vs.
for..of
using mitata in the latest nodejs (v22), as well as in d8 (v8's debug shell) on the latest version (from source) and bench-node to have a third benchmark source.Here's the result in d8:

Latest node:

bench-node:

Source: https://github.com/kurtextrem/foreach-bench
So from the benchmarks (m2 Pro laptop),
forEach
does not seem to be meaningfully slower with large arrays (bench-node result, so guaranteed to be unoptimized), and with mitatas result, forEach even seems slightly faster.Beta Was this translation helpful? Give feedback.
All reactions