Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
fullstack ruleP1universalStack: TypeScript / JavaScript
performancealgorithmsdata-structures
Build a lookup map instead of calling find inside a loop
When you join two collections by key, convert one to a Map once (O(n)) instead of calling Array.find for each item (O(n*m)).
PR: hegnar-user-ws · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codetypescript
| 1 | items.map((item) => { |
| 2 | const detail = details.find((d) => d.insref === item.id); // O(n*m) |
| 3 | return { ...item, ...detail }; |
| 4 | }); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | const detailMap = new Map(details.map((d) => [d.insref, d])); |
| 2 | items.map((item) => { |
| 3 | const detail = detailMap.get(item.id); // O(1) lookups |
| 4 | return { ...item, ...detail }; |
| 5 | }); |
Explanation (EN)
Objašnjenje (HR)