noForEach
Цей контент ще не доступний вашою мовою.
Summary
Section titled “Summary”- Rule available since:
v1.0.0 - Diagnostic Category:
lint/complexity/noForEach - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Same as
unicorn/no-array-for-each - Same as
needless_for_each
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "complexity": { "noForEach": "error" } } }}Description
Section titled “Description”Prefer for...of statement instead of Array.forEach.
Here’s a summary of why forEach may be disallowed, and why for...of is preferred for almost any use-case of forEach:
-
Performance: Using
forEachcan lead to performance issues, especially when working with large arrays. When more requirements are added on,forEachtypically gets chained with other methods likefilterormap, causing multiple iterations over the same Array. Encouraging for loops discourages chaining and encourages single-iteration logic (e.g. using a continue instead offilter). -
Readability: While
forEachis a simple and concise way to iterate over an array, it can make the code less readable, especially when the callback function is complex. In contrast, using a for loop or afor...ofloop can make the code more explicit and easier to read. -
Debugging:
forEachcan make debugging more difficult, because it hides the iteration process.
Caveat
Section titled “Caveat”We consider all objects with a method named forEach to be iterable.
This way, this rule applies to all objects with a method called forEach, not just Array instances.
Exception for Index Usage
Section titled “Exception for Index Usage”When the index is explicitly used in the forEach callback, it is acceptable to use forEach. This is because:
- The index is directly available as the second argument in
forEach, making it convenient for scenarios where the index is necessary. - In sparse arrays,
forEachwill skip undefined entries, which differs from the behavior offor...ofwithObject.entriesthat includes these entries. This can be important for certain array operations, particularly in TypeScript environments with strict type checking.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”els.forEach((el) => { f(el);})code-block.js:1:1 lint/complexity/noForEach ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Prefer for…of instead of forEach.
> 1 │ els.forEach((el) => {
│ ^^^^^^^^^^^^^^^^^^^^^
> 2 │ f(el);
> 3 │ })
│ ^^
4 │
ℹ forEach may lead to performance issues when working with large arrays. When combined with functions like filter or map, this causes multiple iterations over the same type.
els["forEach"](el => { f(el);})code-block.js:1:1 lint/complexity/noForEach ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Prefer for…of instead of forEach.
> 1 │ els[“forEach”](el => {
│ ^^^^^^^^^^^^^^^^^^^^^^
> 2 │ f(el);
> 3 │ })
│ ^^
4 │
ℹ forEach may lead to performance issues when working with large arrays. When combined with functions like filter or map, this causes multiple iterations over the same type.
els.forEach((el, i) => { f(el, i)})for (const el of els) { f(el);}Options
Section titled “Options”The rule provides a validIdentifiers option that allows specific variable names to call forEach.
In the following configuration, it’s allowed to call forEach with expressions that match Effect or _:
{ "linter": { "rules": { "complexity": { "noForEach": { "options": { "allowedIdentifiers": [ "Effect", "_" ] } } } } }}Effect.forEach((el) => { f(el);})_.forEach((el) => { f(el);})Values with dots (e.g., “lib._”) will not be accepted.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.