Skip to content

spreadAccumulators

Reports spread operations that accumulate values in loops, causing quadratic time complexity.

✅ This rule is included in the performance logical presets.

Using spread operations to accumulate values in a loop creates a new array or object on each iteration by copying all previous elements. This results in quadratic time complexity (O(n²)) because each iteration does more work than the last. As the accumulated collection grows, this pattern becomes increasingly inefficient and can significantly slow down your code.

Instead of spreading into a new array or object each iteration, use mutating methods like push() for arrays or direct property assignment for objects. These operations have constant time complexity (O(1)) per iteration, resulting in overall linear time (O(n)) for the entire loop.

AEle53arrAE< =sp ansty="le--0:#D6DEEB;--1:#403F">53 [<];d ivclassNa="meec-li">AEf53< (sp ansty="le--0:#C792EA;--1:#8844">AEconsB0itA7253 items)< {d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">53arraA53< [sp ansty="le--0:#7FDBCA;--1:#096E">72.<..sp ansty="le--0:#D6DEEB;--1:#403F">53array, item<];d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>AEle53numbeAE< =sp ansty="le--0:#D6DEEB;--1:#403F">53 [<];d ivclassNa="meec-li">AEf53< (sp ansty="le--0:#C792EA;--1:#8844">AEle5<3isp ansty="le--0:#C792EA;--1:#8844">AE <= sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp ansty="le--0:#D6DEEB;--1:#403F">53; A5<3 sp ansty="le--0:#F78C6C;--1:#AA09">82<10sp ansty="le--0:#D6DEEB;--1:#403F">53;< isp ansty="le--0:#C792EA;--1:#8844">AE<++sp ansty="le--0:#D6DEEB;--1:#403F">53)< {d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">53numberA53< [sp ansty="le--0:#7FDBCA;--1:#096E">72.<..sp ansty="le--0:#D6DEEB;--1:#403F">53numbers, A5<3 sp ansty="le--0:#F78C6C;--1:#AA09">8<22sp ansty="le--0:#D6DEEB;--1:#403F">53<];d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>AEle53objeAE = <{}sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEf53< (sp ansty="le--0:#C792EA;--1:#8844">AEconst< [sp ansty="le--0:#82AAFF;--1:#3B61">B0kAE<, sp ansty="le--0:#82AAFF;--1:#3B61">B0valAE<] sp ansty="le--0:#7FDBCA;--1:#096E">7253 entries)< {d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">53objecA53 <{ sp ansty="le--0:#7FDBCA;--1:#096E">72.<..sp ansty="le--0:#D6DEEB;--1:#403F">53object, [key]: value <};d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
44AE3F53a443F53< [] ;divclass="Nameec-">44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AEco61B04<4AE spans="tyle--0:#7FDBCA;--1:#09">6E<72of spans="tyle--0:#D6DEEB;--1:#40">3F53 item3F<53 spans="tyle--0:#D6DEEB;--1:#40">3F53a4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B03F53(it3<);><}di vdivclass="Nameexpressive-">44AE3F53num443F53< [] ;divclass="Nameec-">44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AE344A0<9820 spans="tyle--0:#D6DEEB;--1:#40">3F53<; i spans="tyle--0:#C792EA;--1:#88">4<4AE< spans="tyle--0:#D6DEEB;--1:#40">309<8210 spans="tyle--0:#D6DEEB;--1:#40">3F5<3; i spans="tyle--0:#C792EA;--1:#88">443F5<3) {divclass="Nameec-">3F<53 spans="tyle--0:#D6DEEB;--1:#40">3F53num4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B03F5<3(i spans="tyle--0:#C792EA;--1:#88">4<4AE* spans="tyle--0:#D6DEEB;--1:#40">30<9822 spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Nameec-">3<);><}di vdivclass="Nameexpressive-">44AE3F53ob44AE <= {} spans="tyle--0:#D6DEEB;--1:#40">344A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AEcon61B<0key spans="tyle--0:#C792EA;--1:#88">4461B0v446E<72of spans="tyle--0:#D6DEEB;--1:#40">3F53 entrie3F<53 spans="tyle--0:#D6DEEB;--1:#40">3F53object[k4<4AE= spans="tyle--0:#D6DEEB;--1:#40">3F53 va3

This rule is not configurable.

If you need to maintain immutability within a loop and performance is not a concern for your use case, you may choose to disable this rule. For example, in scenarios where you need to preserve each intermediate state of the accumulation for debugging or other purposes.

However, even when immutability is required, consider collecting items first and creating the final immutable collection once after the loop completes, rather than recreating it on every iteration.