Skip to content

Latest commit

Β 

History

History
50 lines (35 loc) Β· 1.64 KB

File metadata and controls

50 lines (35 loc) Β· 1.64 KB

no-array-concat-in-loop

πŸ“ 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).

Examples

// ❌
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);
}