Replace Repeated Search with a Map
Use a lookup map to reduce repeated search over related collections.
Orders repeatedly scan the full customer list to attach customer details. Index once, then look up by key. The shortcut is assuming readable nested code is harmless because the staging sample is small. Find the repeated question For every order, the code asks: which customer has this customerId? Repeated identical questions are candidates for an index. Build the lookup once Create customersById = new Map(customers.map(c => [c.id, c])) before processing orders. One pass organizes the data for cheap retrieval. Replace scan with get For each order, call customersById.get(order.customerId) and handle missing customers explicitly. The lookup is fast and the missing-key…
Sign up free — one personalized lesson every day, matched to your role and goals.
Already have an account? Sign in