Map vs Set vs WeakMap vs WeakSet: When to Use Which (Interview-Ready)
A practical guide to Map/Set and WeakMap/WeakSet: key differences, memory behavior, real use cases, and common interview questions with examples.
Frontend Interview Team
February 08, 2026
If an interviewer asks “Map vs Object?” or “What is WeakMap?” they’re testing whether you understand:
- key types
- iteration
- memory/garbage collection behavior
This post keeps it simple and interview-ready.
30‑second interview answer
Use Map for key-value pairs when keys aren’t just strings and you need predictable iteration. Use Set for unique values. Use WeakMap/WeakSet when keys must be objects and you don’t want the data structure to prevent garbage collection—useful for caching metadata on objects without causing leaks.
Key points
Map: any key type, ordered iteration,size.Set: unique values.WeakMap/WeakSet: keys are objects only, not iterable, weakly held (GC-friendly).
1) Map (key-value store)
Key points
- Keys can be any type (objects, functions, numbers)
- Preserves insertion order
- Easy size:
map.size - Iteration is straightforward
const map = new Map();
map.set('a', 1);
map.set(10, 'ten');
map.set({ id: 1 }, 'object key');
console.log(map.get('a')); // 1
console.log(map.size); // 3When to use Map
- dynamic key sets
- you need non-string keys
- frequent add/remove operations
2) Set (unique values)
Key points
- Stores unique values
- Great for “does this exist?” checks
const s = new Set([1, 2, 2, 3]);
console.log(s); // Set {1,2,3}
s.add(4);
console.log(s.has(2)); // true
s.delete(1);When to use Set
- de-duplication
- membership checks
- tracking visited items
3) WeakMap (object keys, weakly held)
Key points
- Keys must be objects (not strings/numbers)
- Keys are held weakly
- if nothing else references the key object, it can be garbage collected
- Not iterable (no
.keys(), no.size)
const wm = new WeakMap();
let user = { id: 1 };
wm.set(user, { lastSeen: Date.now() });
// later...
user = null;
// if no other refs exist, the key object can be GC'd and entry disappearsWhen to use WeakMap
- attaching metadata to objects without preventing GC
- caching results per object without memory leaks
Real example: per-DOM-element metadata
const meta = new WeakMap();
function track(el) {
meta.set(el, { clicks: 0 });
el.addEventListener('click', () => {
meta.get(el).clicks++;
});
}If the element is removed and no longer referenced, WeakMap won’t keep it alive.
4) WeakSet (weakly held objects)
Key points
- Values must be objects
- Values are held weakly
- Not iterable
const ws = new WeakSet();
let obj = { name: 'x' };
ws.add(obj);
console.log(ws.has(obj)); // true
obj = null;When to use WeakSet
- tracking “seen objects” without preventing GC
5) Map vs Object (common interview question)
Use Object when
- you want simple records/dicts with string keys
- JSON compatibility matters
Use Map when
- keys are not strings
- you need reliable iteration order
- frequent additions/removals
Also: Objects have prototypes; Maps don’t have the same key collision risks.
6) Quick comparison table (in words)
- Map: any keys, iterable, has size
- Set: unique values, iterable, has size
- WeakMap: object keys only, not iterable, weak refs (GC friendly)
- WeakSet: object values only, not iterable, weak refs
7) Interview Q&A
Q: Why is WeakMap not iterable?
- Because entries can disappear anytime when GC runs. Iteration would be unreliable.
Q: Can WeakMap keys be primitives?
- No. Only objects.
Q: How does WeakMap help prevent memory leaks?
- It doesn’t keep object keys alive. If the key object becomes unreachable elsewhere, GC can reclaim it.
Summary checklist
- I can compare Map vs Object.
- I know WeakMap keys must be objects.
- I can explain why WeakMap isn’t iterable.
- I can describe a GC-friendly metadata/cache use case.
Summary
- Map/Set are your go-to modern data structures.
- WeakMap/WeakSet are for object-keyed metadata/caches that should not prevent garbage collection.
- If you can explain the weak reference idea clearly, you’ll do great in interviews.