Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Do not clone an object before an operation that does not mutate it
Skip defensive clones (e.g. before reduce, find, map) when the subsequent operation is non-mutating and the clone serves no purpose.
Bad example
| 1 | const copy = { ...channels }; |
| 2 | return Object.values(copy).reduce(toResult, []); |
Explanation (EN)
Copying ordinary unchanged data before a pure read adds allocation with no contract benefit.
Objašnjenje (HR)
Good example
| 1 | return Object.values(channels).reduce(toResult, []); |
Explanation (EN)
Objašnjenje (HR)
Notes (EN)
Before removing a shallow clone ahead of a nonmutating operation, verify callback behavior, accessors/proxies and whether an independent snapshot or identity is required.
Exceptions / Tradeoffs (EN)
map/reduce/find do not mutate their receiver by themselves, but callbacks may mutate it; a spread can also invoke getters or strip a prototype.