``` # Avoid O(n^2) operations on large collections Nested iteration over the same large collection (e.g.
Shoppingby Patrick2026-09-17
# Avoid O(n^2) operations on large collections
Nested iteration over the same large collection (e.g. `.find()` inside a
`.map()` over the same array) should be replaced with a lookup structure
(e.g. a `Map`) when a linear-time approach is available.
const byId = new Map(orders.map((o) => [o.id, o]));
const enriched = ids.map((id) => byId.get(id));
Bad:
const enriched = ids.map((id) => orders.find((o) => o.id === id));
```