TypeScript · performance
no-string-concat-in-loop
eslint:no-string-concat-in-loop Disallow O(n^2) string building via `+=` on a string variable inside a loop; push parts to an array and `join` instead.
- Default
- error
- Fix
- none
- Languages
- typescript
Why
Repeatedly rebuilding a growing string can copy all prior content on each iteration, making total work grow quadratically.
Fix
Collect each fragment in an array, then join the fragments after the loop.
Before / after
Executed by this rule’s unit tests.
Before
Do not rebuild a growing string in a loop
let output = ''; for (const item of items) { output = `${output}${item}`; } After
Join collected fragments after the loop
const parts = []; for (const item of items) { parts.push(item); } const output = parts.join(""); Limits
- Only local identifiers initialized with a string or template literal and accumulated in a loop body are inspected.
Message IDs
noStringConcatInLoop