no-accumulating-copy: [...acc, x] on every step of a loop or reduce #3
Loading…
Reference in a new issue
No description provided.
Delete branch "no-accumulating-copy"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
New rule
no-accumulating-copy(warning in the recommended preset):[...acc, x],{ ...acc, [k]: v }oracc.concat(x)that copies the whole collection on every step.What it reports
reduce/reduceRightcallback: 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(...).for,for...of,for...in,while,do, andforEachcallbacks): 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 localMap. 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
comparebench (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:Preset
oxc/no-accumulating-spreadis removed fromrecommended.json. It reports the samereducespreads, so the editor would show them twice. This rule also runs in the CLI, the Vite plugin and the playground, and coversconcat. If you'd rather keep both, putting the line back is enough.Registered in
lints.rs,lib.rsRULES, oxlint pluginindex.mjsandrecommended.json, fixturelints/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 isfixtures_match_annotationson fixturefeatures(main.ts:55,createDecipherivis now reported). It fails the same way onmain(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 oncefails with "stale" node:sqlite/node:path entries, also unrelated (same@types/nodedrift).npm run test:vite: passes.npm run test:docs: passes (217 examples compile, site builds).docs.rschecks the new page's good and bad blocks.safe-ts checkonexamples/demoandexamples/vuegives nono-accumulating-copyreports, 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 areduceinto a loop isn't a mechanical edit in general.🤖 Generated with 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]>