π Disallow array accumulation with Array#concat() in loops.
πΌπ« This rule is enabled in the β
recommended config. This rule is disabled in the βοΈ unopinionated config.
Array#concat() creates a new array every time it is called. When an accumulator is reassigned with .concat() on every loop iteration, each iteration copies everything accumulated so far, which can make the loop quadratic in the total number of accumulated items.
Use Array#push() inside the loop when each chunk is an array, or collect the nested arrays and flatten them once outside the loop.
This rule intentionally only reports local let and var variables initialized to an empty array literal. It does not try to infer arbitrary array-like values or custom .concat() methods.
This rule does not provide an autofix. Replacing .concat() with .push() can change observable behavior when the old array is aliased, and .concat() has argument-spreading semantics that are not always equivalent to push(...value).
// β
let result = [];
for (const chunk of chunks) {
result = result.concat(chunk);
}// β
const result = [];
for (const chunk of chunks) {
result.push(...chunk);
}// β
const result = chunks.flat();// β
let result = [];
for (const chunk of chunks) {
result = other.concat(chunk);
}