{"record":{"id":"998591c151c0fe33","repo":"oxc-project/oxc","slug":"do-not-spread-accumulators-in-loops","errorCode":null,"errorMessage":"Do not spread accumulators in loops","messagePattern":"Do not spread accumulators in loops","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/oxc/no_accumulating_spread.rs","lineNumber":55,"sourceCode":"        ])\n}\n\nfn reduce_unknown(spread_span: Span, reduce_span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Do not spread accumulators in Array.prototype.reduce()\")\n        .with_help(\"Consider using `Object.assign()` or `Array.prototype.push()` to mutate the accumulator instead.\")\n        .with_note(\"Using spreads within accumulators leads to `O(n^2)` time complexity.\")\n        .with_labels([\n            spread_span.label(\"From this spread\"),\n            reduce_span.label(\"For this reduce\")\n        ])\n}\n\nfn loop_spread_likely_object_diagnostic(\n    accumulator_decl_span: Span,\n    spread_span: Span,\n    loop_span: Span,\n) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Do not spread accumulators in loops\")\n        .with_help(\"Consider using `Object.assign()` to mutate the accumulator instead.\")\n        .with_note(\"Using spreads within accumulators leads to `O(n^2)` time complexity.\")\n        .with_labels([\n            accumulator_decl_span.label(\"From this accumulator\"),\n            spread_span.label(\"From this spread\"),\n            loop_span.primary_label(\"For this loop\"),\n        ])\n}\nfn loop_spread_likely_array_diagnostic(\n    accumulator_decl_span: Span,\n    spread_span: Span,\n    loop_span: Span,\n) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Do not spread accumulators in loops\")\n        .with_help(\"Consider using `Array.prototype.push()` to mutate the accumulator instead.\")\n        .with_note(\"Using spreads within accumulators leads to `O(n^2)` time complexity.\")\n        .with_labels([\n            accumulator_decl_span.label(\"From this accumulator\"),","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/oxc/no_accumulating_spread.rs#L37-L73","documentation":"Oxlint performance rule `oxc/no_accumulating_spread`, loop variant: an object accumulator declared outside a loop is reassigned with an object spread (`{ ...acc, ... }`) inside the loop body. Each iteration copies all previously accumulated properties, so total work is O(n^2) in the number of iterations. The rule suggests `Object.assign()` mutation instead, and labels the accumulator declaration, the spread, and the loop (primary).","triggerScenarios":"Code shaped like `let acc = {}; for (const x of xs) { acc = { ...acc, [x.key]: x.value }; }` (or while/do-while), where the accumulator is declared before the loop, the analysis concludes it is likely an object, and it is spread inside the loop.","commonSituations":"Merging per-item results into a single object inside for-of loops; incremental config/props building in React render loops; enabling the `oxc` plugin's performance rules on existing code for the first time.","solutions":["Mutate in place: `Object.assign(acc, { [x.key]: x.value })` or plain `acc[x.key] = x.value` inside the loop","Use a `Map` during the loop and convert with `Object.fromEntries(map)` once after it","If immutability per iteration is required (e.g. snapshots), disable the rule locally and document why the quadratic cost is acceptable"],"exampleFix":"// before\nlet acc = {};\nfor (const x of xs) {\n  acc = { ...acc, [x.key]: x.value };\n}\n\n// after\nconst acc = {};\nfor (const x of xs) {\n  acc[x.key] = x.value;\n}","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["In loops, mutate accumulators (Object.assign or direct assignment) instead of reassigning spreads","Use Map/Set for accumulation and convert once after the loop","Run `oxlint` with the oxc plugin in CI to catch accumulating spreads before merge"],"tags":["oxlint","performance","loop","object-spread","quadratic-complexity"],"backgroundTag":"spread-in-loop-quadratic-copy","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}