Skip to content

no-reduce-accumulator-copy

Review copies of the accumulated collection inside a built-in reduce callback.

Why

Copying a growing accumulator at every step can make collection quadratic instead of linear in its output size.

Fix

Use map or flatMap where their semantics match, or append into a fresh locally owned accumulator. Preserve retained snapshots and shared state.

Examples

Before — flagged Repeated concatenation copies prior report rows
src/reports.ts
declare const pages: string[][];
const rows = pages.reduce<string[]>((acc, page) => acc.concat(page), []);
After — preferred Flatten report rows without copying the accumulated prefix
src/reports.ts
declare const pages: string[][];
const rows = pages.flatMap((page) => page);