no-accumulating-copy: [...acc, x] on every step of a loop or reduce #3

Merged
sionzee merged 1 commit from no-accumulating-copy into main 2026-11-03 21:27:31 +01:00
Owner

New rule no-accumulating-copy (warning in the recommended preset): [...acc, x], { ...acc, [k]: v } or acc.concat(x) that copies the whole collection on every step.

What it reports

  • In a reduce/reduceRight callback: an array literal that spreads the accumulator and adds something ([...acc, x], [x, ...acc]), an object literal that spreads it and has a computed key ({ ...acc, [k]: v }), acc.concat(...).
  • In a loop (for, for...of, for...in, while, do, and forEach callbacks): the same copies assigned back to what they copied: list = [...list, x], state.items = state.items.concat(more), this.items = [...], where the target lives outside the loop.

Help text: push into one local array and return it (a helper that adds to it takes Mut<T[]>); for objects, fill a local Map. That's the state guide's "pass state in, return the new state" with the array private until it's returned.

Not reported (in the fixture): a single copy like the cart example ({ ...cart, items: [...cart.items, item] }), copies that don't grow ({ ...acc, count: acc.count + 1 }, [...acc] alone), and variables made fresh inside the loop.

Numbers (rule page, ```bench block)

The page has a compare bench (spread vs push, 1,000 orders) that runs in the browser like the Shapes one. I replayed the built page's bench spec in Node to check both variants run and return the same result. Measured in Node 26.13 with the worker's method (batches of at least 25 ms, 12 rounds, median), three runs:

spread push / Map ratio
array, 1,000 377–493 µs 3.1–3.5 µs ~125–145x
array, 10,000 41–50 ms 32–37 µs ~1,300x
object by id, 1,000 74–78 ms 14 µs (Map) ~5,000x

Preset

oxc/no-accumulating-spread is removed from recommended.json. It reports the same reduce spreads, so the editor would show them twice. This rule also runs in the CLI, the Vite plugin and the playground, and covers concat. If you'd rather keep both, putting the line back is enough.

Registered in

lints.rs, lib.rs RULES, oxlint plugin index.mjs and recommended.json, fixture lints/main.ts, docs/rules/no-accumulating-copy.md, docs/README.md, the README rule table, the sidebar (Performance), and a line in performance-myths.md.

Tests

  • cargo nextest run --workspace: 94/95 pass. The failure is fixtures_match_annotations on fixture features (main.ts:55, createDecipheriv is now reported). It fails the same way on main (0e01fb3) in this checkout, so it's not from this change. It looks like drift from the installed @types/node.
  • npm run test:std: 115/116. every Node callable is mapped exactly once fails with "stale" node:sqlite/node:path entries, also unrelated (same @types/node drift).
  • npm run test:vite: passes. npm run test:docs: passes (217 examples compile, site builds). docs.rs checks the new page's good and bad blocks.
  • Noise: safe-ts check on examples/demo and examples/vue gives no no-accumulating-copy reports, and nor do any docs examples.

Left out / can't see (documented on the page)

A copy made inside a called function (cart = addItem(cart, x) in a loop), copying into another variable first (const next = [...acc]; next.push(x)), Object.assign({}, acc, …), and { ...acc, ...other }. There's no quick fix: turning a reduce into a loop isn't a mechanical edit in general.

🤖 Generated with Claude Code

New rule `no-accumulating-copy` (warning in the recommended preset): `[...acc, x]`, `{ ...acc, [k]: v }` or `acc.concat(x)` that copies the whole collection on every step. ## What it reports - In a `reduce`/`reduceRight` callback: an array literal that spreads the accumulator and adds something (`[...acc, x]`, `[x, ...acc]`), an object literal that spreads it and has a computed key (`{ ...acc, [k]: v }`), `acc.concat(...)`. - In a loop (`for`, `for...of`, `for...in`, `while`, `do`, and `forEach` callbacks): the same copies **assigned back to what they copied**: `list = [...list, x]`, `state.items = state.items.concat(more)`, `this.items = [...]`, where the target lives outside the loop. Help text: push into one local array and return it (a helper that adds to it takes `Mut<T[]>`); for objects, fill a local `Map`. That's the state guide's "pass state in, return the new state" with the array private until it's returned. Not reported (in the fixture): a single copy like the cart example (`{ ...cart, items: [...cart.items, item] }`), copies that don't grow (`{ ...acc, count: acc.count + 1 }`, `[...acc]` alone), and variables made fresh inside the loop. ## Numbers (rule page, ```bench block) The page has a `compare` bench (spread vs push, 1,000 orders) that runs in the browser like the Shapes one. I replayed the built page's bench spec in Node to check both variants run and return the same result. Measured in Node 26.13 with the worker's method (batches of at least 25 ms, 12 rounds, median), three runs: | | spread | push / Map | ratio | |---|---|---|---| | array, 1,000 | 377–493 µs | 3.1–3.5 µs | ~125–145x | | array, 10,000 | 41–50 ms | 32–37 µs | ~1,300x | | object by id, 1,000 | 74–78 ms | 14 µs (Map) | ~5,000x | ## Preset `oxc/no-accumulating-spread` is removed from `recommended.json`. It reports the same `reduce` spreads, so the editor would show them twice. This rule also runs in the CLI, the Vite plugin and the playground, and covers `concat`. If you'd rather keep both, putting the line back is enough. ## Registered in `lints.rs`, `lib.rs` RULES, oxlint plugin `index.mjs` and `recommended.json`, fixture `lints/main.ts`, `docs/rules/no-accumulating-copy.md`, `docs/README.md`, the README rule table, the sidebar (Performance), and a line in performance-myths.md. ## Tests - `cargo nextest run --workspace`: 94/95 pass. The failure is `fixtures_match_annotations` on fixture `features` (`main.ts:55`, `createDecipheriv` is now reported). It fails the same way on `main` (0e01fb3) in this checkout, so it's not from this change. It looks like drift from the installed `@types/node`. - `npm run test:std`: 115/116. `every Node callable is mapped exactly once` fails with "stale" node:sqlite/node:path entries, also unrelated (same `@types/node` drift). - `npm run test:vite`: passes. `npm run test:docs`: passes (217 examples compile, site builds). `docs.rs` checks the new page's good and bad blocks. - Noise: `safe-ts check` on `examples/demo` and `examples/vue` gives no `no-accumulating-copy` reports, and nor do any docs examples. ## Left out / can't see (documented on the page) A copy made inside a called function (`cart = addItem(cart, x)` in a loop), copying into another variable first (`const next = [...acc]; next.push(x)`), `Object.assign({}, acc, …)`, and `{ ...acc, ...other }`. There's no quick fix: turning a `reduce` into a loop isn't a mechanical edit in general. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
`reduce((acc, x) => [...acc, x], [])`, `{ ...acc, [k]: v }` and `acc.concat(x)` copy
everything collected so far on each step, so collecting n items copies about n²/2 of them:
for 1,000 items about 130x slower than pushing into a local array, for 10,000 about 1,300x
(Node 26, measured with the bench on the rule page). Loops are covered when the copy is
assigned back to what it copied (`list = [...list, x]`, `state.items = state.items.concat(..)`),
including forEach callbacks.

Copies that don't grow (`{ ...acc, count: acc.count + 1 }`), a single spread outside a loop
(the cart example) and variables made fresh inside the loop are left alone. The rule replaces
oxc/no-accumulating-spread in the recommended preset, which would report the same spreads
twice; this one also runs in the CLI, the Vite plugin and the playground, and covers concat.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch no-accumulating-copy 2026-11-03 21:27:31 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!3
No description provided.