Skip to content

@studnicky/v8/array-spread-outside-loops

Disallows array spread ([...result, item]) in assignment inside for loop bodies. Each spread creates a new array and copies all existing elements, producing O(n²) work. Accumulate with .push() instead.

Fixable: No · Options: No · Suggested severity: error

✗ Incorrect

ts
let result: string[] = [];
for (const item of items) {
  result = [...result, item];
}
ts
let merged: number[] = [];
for (let i = 0; i < pages.length; i++) {
  merged = [...merged, ...pages[i]];
}

✓ Correct

ts
const result: string[] = [];
for (const item of items) {
  result.push(item);
}
ts
// Spread outside loop is fine
const merged = pages.flat();