noAccumulatingSpread
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/performance/noAccumulatingSpread¥Diagnostic Category:
lint/performance/noAccumulatingSpread -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
¥How to configure
{ "linter": { "rules": { "performance": { "noAccumulatingSpread": "error" } } }}¥Description
禁止在累加器上使用扩展 (...) 语法。
¥Disallow the use of spread (...) syntax on accumulators.
扩展语法允许将可迭代对象扩展为其各个元素。
¥Spread syntax allows an iterable to be expanded into its individual elements.
应避免在累加器(如 .reduce 中的累加器)上使用扩展语法,因为它会导致时间复杂度为 O(n^2) 而不是 O(n)。
¥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).
来源:https://prateeksurana.me/blog/why-using-object-spread-with-reduce-bad-idea/
¥Source: https://prateeksurana.me/blog/why-using-object-spread-with-reduce-bad-idea/
¥Examples
¥Invalid
var 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.
var a = ['a', 'b', 'c'];a.reduce((acc, val) => {return Object.assign([], acc, val);}, []);code-block.js:2:32 lint/performance/noAccumulatingSpread ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Avoid the use of Object.assign on accumulators.
1 │ var a = [‘a’, ‘b’, ‘c’];
> 2 │ a.reduce((acc, val) => {return Object.assign([], 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.
¥Valid
var a = ['a', 'b', 'c'];a.reduce((acc, val) => {acc.push(val); return acc}, []);var a = ['a', 'b', 'c'];a.reduce((acc, val) => Object.assign(acc, val), []);var a = ['a', 'b', 'c'];a.reduce((acc, val) => {return Object.assign(acc, val);}, []);¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号