Skip to content

oxc/no-accumulating-spread Perf

What it does

Prevents using object or array spreads on accumulators in Array.prototype.reduce() and in loops.

Why is this bad?

Object and array spreads create a new object or array on each iteration. In the worst case, they also cause O(n) copies (both memory and time complexity). When used on an accumulator, this can lead to O(n^2) memory complexity and O(n^2) time complexity.

For a more in-depth explanation, see this blog post by Prateek Surana.

Examples

Examples of incorrect code for this rule:

javascript
arr.reduce((acc, x) => ({ ...acc, [x]: fn(x) }), {});
Object.keys(obj).reduce((acc, el) => ({ ...acc, [el]: fn(el) }), {});

let foo = [];
for (let i = 0; i < 10; i++) {
  foo = [...foo, i];
}

Examples of correct code for this rule:

javascript
function fn(x) {
  // ...
}

arr.reduce((acc, x) => acc.push(fn(x)), []);
Object.keys(obj).reduce((acc, el) => {
  acc[el] = fn(el);
}, {});
// spreading non-accumulators should be avoided if possible, but is not
// banned by this rule
Object.keys(obj).reduce((acc, el) => {
  acc[el] = { ...obj[el] };
  return acc;
}, {});

let foo = [];
for (let i = 0; i < 10; i++) {
  foo.push(i);
}

References

Released under the MIT License.