Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Mutate the existing array when grouping into a Map, don't spread-copy each push
Append to the array already stored in the Map rather than replacing it with a spread-built copy on every iteration.
Bad example
| 1 | const groups = new Map<string, Item[]>(); |
| 2 | for (const row of rows) { |
| 3 | const existing = groups.get(row.key) ?? []; |
| 4 | groups.set(row.key, [...existing, row.value]); |
| 5 | } |
Explanation (EN)
Each iteration copies the whole existing array into a new one via spread, making grouping O(n^2) for large inputs and creating churn for the garbage collector.
Objašnjenje (HR)
Svaka iteracija kopira cijeli postojeći niz u novi pomoću spread operatora, čineći grupiranje O(n^2) za velike ulaze i stvarajući opterećenje za sakupljač smeća.
Good example
| 1 | const groups = new Map<string, Item[]>(); |
| 2 | for (const row of rows) { |
| 3 | if (!groups.has(row.key)) groups.set(row.key, []); |
| 4 | groups.get(row.key)!.push(row.value); |
| 5 | } |
Explanation (EN)
We create the array once and push into it in place, so grouping stays O(n) and no intermediate arrays are allocated.
Objašnjenje (HR)
Niz stvaramo jednom i ubacujemo u njega na mjestu, pa grupiranje ostaje O(n) i ne alociraju se međuspremni nizovi.
Exceptions / Tradeoffs (EN)
Spreading is fine for a one-off small array or when immutability is required; the concern is repeated copying inside a loop. Balance against prefer-non-mutating-array-truncation: mutate in place when appending into a Map-grouped accumulator you own locally, to avoid per-push copies.
Iznimke / Tradeoffi (HR)
Spread je u redu za jednokratni mali niz ili kad se zahtijeva nepromjenjivost; problem je ponovljeno kopiranje unutar petlje.