Skip to content
Sarj Code Standards

no-growing-array-spread-in-loop

Review repeated prefix copies of a growing fresh local array inside a loop.

Why

Repeatedly copying all accumulated elements can make append collection quadratic in the output size.

Fix

Append into a fresh locally owned output when snapshot and observation semantics permit it.

Examples

Before — flagged Repeated spread copies the accumulated rows
src/rows.ts
export function collect(input: string[]) {
let rows: string[] = [];
for (const row of input) {
rows = [...rows, row];
}
return rows;
}
After — preferred Append into a fresh unobserved output
src/rows.ts
export function collect(input: string[]) {
const rows: string[] = [];
for (const row of input) {
rows.push(row);
}
return rows;
}

References