Stop Looping with find(): When Map Actually Earns Its Place in Frontend Code
Nested find() calls in loops turn O(n) lookups into O(n²) scans that degrade under real data volumes. Recognizing the signal—array.map() containing array.find() by id—and reaching for Map eliminates that cost and clarifies which data owns the truth at any moment.
Most frontend data flows as arrays and plain objects, so Map stays on the bench. Three concrete scenarios show where it flips from overkill to the right tool: simple checkbox forms stay with id arrays; multi-select needing full objects moves to Map<id, item>; and the real payoff is merging API refreshes with unsaved user edits via Map<orderId, editData>. The pattern is always the same—build the index once, then get() instead of find().
The order-merge case is the most instructive. A batch collection table pulls fresh API rows while preserving user-filled amounts and remarks across refreshes. Storing only the user-edited fields in a Map, then spreading API data first and user edits second, keeps v-model from mutating source data and prevents stale API fields from overwriting fresh server state. The rule is simple: never save the whole row into the Map; save only what the user actually touches.
Map's real value in frontend isn't as a data structure for storage—it's as a temporary index that decouples API truth from UI state, letting each refresh freely without losing in-progress edits.
The advice to store only user-touched fields in the Map, not the whole row, is a small rule that prevents a whole class of stale-data bugs that are otherwise hard to trace.
Many developers avoid Map because JSON serialization is awkward, but the scenarios shown here never need to serialize the Map—it lives as ephemeral UI state, which is exactly where it fits.