noAccumulatingSpread
Summary
Section titled Summary- Rule available since:
v1.0.0 - Diagnostic Category:
lint/performance/noAccumulatingSpread - This rule is recommended, which means is enabled by default.
- This rule doesn’t have a fix.
- The default severity of this rule is error.
Description
Section titled DescriptionDisallow the use of spread (...) syntax on accumulators.
Spread syntax allows an iterable to be expanded into its individual elements.
Spread syntax should be avoided on accumulators (like those in .reduce)
because it causes a time complexity of O(n^2) instead of O(n).
Source: https://prateeksurana.me/blog/why-using-object-spread-with-reduce-bad-idea/
Examples
Section titled ExamplesInvalid
Section titled Invalidvar a = ['a', 'b', 'c'];a.reduce((acc, val) => [...acc, val], []);code-block.js:2:25 lint/performance/noAccumulatingSpread ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Avoid the use of spread (...) syntax on accumulators.
1 │ var a = [‘a’, ‘b’, ‘c’];
> 2 │ a.reduce((acc, val) => […acc, val], []);
│ ^^^^^^
3 │
ℹ Spread syntax should be avoided on accumulators (like those in .reduce) because it causes a time complexity of O(n^2).
ℹ Consider methods such as .splice or .push instead.
var a = ['a', 'b', 'c'];a.reduce((acc, val) => {return [...acc, val];}, []);code-block.js:2:33 lint/performance/noAccumulatingSpread ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Avoid the use of spread (...) syntax on accumulators.
1 │ var a = [‘a’, ‘b’, ‘c’];
> 2 │ a.reduce((acc, val) => {return […acc, val];}, []);
│ ^^^^^^
3 │
ℹ Spread syntax should be avoided on accumulators (like those in .reduce) because it causes a time complexity of O(n^2).
ℹ Consider methods such as .splice or .push instead.
var a = ['a', 'b', 'c'];a.reduce((acc, val) => ({...acc, [val]: val}), {});code-block.js:2:26 lint/performance/noAccumulatingSpread ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Avoid the use of spread (...) syntax on accumulators.
1 │ var a = [‘a’, ‘b’, ‘c’];
> 2 │ a.reduce((acc, val) => ({…acc, [val]: val}), {});
│ ^^^^^^
3 │
ℹ Spread syntax should be avoided on accumulators (like those in .reduce) because it causes a time complexity of O(n^2).
ℹ Consider methods such as .splice or .push instead.
Valid
Section titled Validvar a = ['a', 'b', 'c'];a.reduce((acc, val) => {acc.push(val); return acc}, []);How to configure
Section titled How to configure{ "linter": { "rules": { "performance": { "noAccumulatingSpread": "error" } } }}