For simple cases, a plain object does the job fine as a place to stash key-value pairs. Map and Set earn their keep once that stops being true.
const byId = new Map(candidates.map(c => [c.id, c]));
byId.get("c3").name; // "Amara Chen" — direct lookup, no scanning the array
byId.has("c6"); // false
byId.size; // 5 — a real property, not a method call like Object.keys(obj).length
for (const [id, candidate] of byId) { // Map is directly iterable, in INSERTION order
console.log(id, candidate.name);
}
Building byId once turns "find the candidate with this id" from an O(n) scan through candidates.find(...) into an O(1) lookup — worth doing the moment you're looking things up by id more than once.
| Question | Plain object | Map |
|---|---|---|
| What's allowed as a key? | Only a string or a Symbol — anything else gets silently turned into a string first | Anything at all — an object, a function, even another Map |
| How do you find out how many entries there are? | Count them: Object.keys(obj).length | Just ask: .size |
| What order do entries come back in? | Roughly insertion order, except keys that look like array indices jump ahead of everything else, sorted numerically | Exactly the order they went in, with no special-case reshuffling |
| Can you loop over it directly? | Not without going through Object.entries() or similar first | Yes — hand it straight to for...of |
| Any risk of a key stepping on something the language already defined? | Yes — a candidate id that happened to be the literal string "constructor" would land on a property Object.prototype already owns, confusing a careless for...in or .hasOwnProperty check | No — a Map's keys live in their own storage, entirely outside any prototype chain |
A Set keeps at most one copy of any given value, checked with that same SameValueZero comparison .includes() relies on — which is exactly what makes it the natural tool for stripping duplicates out of an array. Every distinct skill tag across the whole candidate pool, say:
const allTags = candidates.flatMap(c => c.tags); // 10 tags total, with repeats
const uniqueTags = [...new Set(allTags)];
// ["node", "postgres", "react", "css", "redis", "typescript"] — 6 unique