Skip to content

no-accumulating-copy ​

[...acc, x], { ...acc, [key]: value } or acc.concat(x) on every step of a reduce, or assigned back to acc on every turn of a loop.

Why: each step copies everything collected so far, so collecting n items copies about n²/2 of them. It looks like one cheap line and it is fine for ten items; for a few thousand it is the slowest code in the program. A local array that you push into and return is just as safe: nobody else sees it until it's done (State).

❌ Rejected: the list is copied once per order

ts
export type Order = { readonly id: string; readonly paid: boolean };

export function paidIds(orders: readonly Order[]): readonly string[] {
  return orders.reduce<readonly string[]>((ids, o) => (o.paid ? [...ids, o.id] : ids), []);
}

✅ Accepted: one array, filled and returned

✓ Accepted0 diagnostics
ts
export type Order = { readonly id: string; readonly paid: boolean };

export function paidIds(orders: readonly Order[]): readonly string[] {
  const ids: string[] = [];
  for (const o of orders) if (o.paid) ids.push(o.id);
  return ids;
}
Benchmark paidIds(orders)
Code that runs

Each version runs on its own, in a fresh copy of this code (TypeScript types are removed first). The setup runs once per version; the last line is what is timed.

Spread on every step
export type Order = { readonly id: string; readonly paid: boolean };

export function paidIds(orders: readonly Order[]): readonly string[] {
  return orders.reduce<readonly string[]>((ids, o) => (o.paid ? [...ids, o.id] : ids), []);
}
Push
export type Order = { readonly id: string; readonly paid: boolean };

export function paidIds(orders: readonly Order[]): readonly string[] {
  const ids: string[] = [];
  for (const o of orders) if (o.paid) ids.push(o.id);
  return ids;
}
Setup and timed expression
const orders = Array.from({ length: 1000 }, (_, i) => ({ id: `o${i}`, paid: i % 4 !== 0 }));

// timed:
paidIds(orders);

Warm: median time per run once V8 has optimized the code (p10–p90: the spread of the samples). First run: the very first call, before any optimization, which is closer to code that runs rarely. Measured in a Web Worker in your browser; results depend on the engine and the machine, which is the point of running it here.
Size: Spread on every step 235 B (127 B gzip) · Push 239 B (129 B gzip).

Measured on Node 26: for 1,000 orders, 380–490 µs with the spread against 3.1–3.5 µs with push, about 130x. The gap grows with the input: for 10,000 it is 41–50 ms against 32–37 µs, about 1,300x.

Returning ids as readonly string[] keeps callers from changing it. If a helper adds to the array, give its parameter the type Mut<string[]>, so the signature says so (no-param-mutation).

Objects built key by key are worse, because the copy reads every key by name:

ts
export type User = { readonly id: string; readonly name: string };

export function byId(users: readonly User[]): Readonly<Record<string, User>> {
  return users.reduce<Readonly<Record<string, User>>>((acc, u) => ({ ...acc, [u.id]: u }), {});
}
✓ Accepted0 diagnostics
ts
export type User = { readonly id: string; readonly name: string };

export function byId(users: readonly User[]): ReadonlyMap<string, User> {
  const found = new Map<string, User>();
  for (const u of users) found.set(u.id, u);
  return found;
}

For 1,000 users: 74–78 ms with the spread, 14 µs with the Map (Node 26). A Map is also what no-object-dictionary asks for when keys come from data.

The same copy in a loop is reported when it is assigned back to what it copied: list = [...list, x], state.items = state.items.concat(more), and in a forEach callback.

Not reported: one copy that isn't repeated, like { ...cart, items: [...cart.items, item] } in a function that adds one item; a copy that doesn't grow ({ ...acc, count: acc.count + 1 } has the same keys every step); a variable made fresh inside the loop.

Can't see: a copy made through a function call (cart = addItem(cart, x) in a loop, where addItem spreads), a copy into another variable first (const next = [...acc]; next.push(x); return next;), Object.assign({}, acc, ...), and { ...acc, ...other } (whether that grows depends on other's keys).

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra