Under The Hood
frontend javascript ES2015+ (WeakMap/WeakSet semantics)

WeakMap / WeakSet: Ephemerons & Garbage Collector Reclamation

Last updated
Prerequisites:
How Map and Set work
Basics of garbage collection and object lifetimes
  • weakmap
  • weakset
  • ephemeron
  • garbage-collection
  • memory
  • weak-reference

Read at your depth

The practical view

WeakMap stores key → value pairs where the key is an object (or symbol) held 'weakly': the key does not keep the object alive. If the key object becomes unreachable, the entry is removed automatically, so you cannot leak memory by leaving stale metadata around. WeakSet is the same idea for a set of objects. Practical uses: caching computed results keyed by objects, storing private data per-instance (like a symbol table), and tracking 'seen' objects without preventing their collection. Because keys are weak, you cannot iterate a WeakMap or check its size — the API is deliberately limited (get, set, has, delete).

The same idea in other frameworks

react equivalent
React internals have used WeakMap-based caches for memoized data keyed by component/fiber objects.

Legacy vs modern

Per-instance metadata: Map (strong) vs WeakMap (weak) for private state

A WeakMap keyed by the instance lets the instance be collected with its metadata; a Map keyed by the instance pins both forever.

before → after
Map keyed by object → pins memory
const cache = new Map(); // ❌ OLD: strong key → object kept alive
cache.set(user, expensiveMeta); // user can never be GC'd while cache lives
WeakMap keyed by object → auto-reclaimed
const cache = new WeakMap(); // ✅ NEW: weak key → entry dies with the key
cache.set(user, expensiveMeta); // when `user` dies, entry is collected

Interview gotchas

Context

This tests whether you understand that the API restrictions are not arbitrary — they are a direct consequence of how ephemerons must be collected.

The mechanical answer

WeakMap keys are held weakly, so their identity can vanish at any GC cycle. If iteration or .size were available, the engine would have to keep a stable, enumerable list of live keys — which forces it to maintain strong references to them to guarantee the list remains consistent across collection. That defeats the entire purpose. The semantics require that a key's presence in the WeakMap never affect its reachability, so the only observable operations are get/set/has/delete, which do not require maintaining a stable snapshot of the key set. The engine uses an ephemeron hash table where keys are weak references and entries are swept when the key dies.

Trap

The naive answer is 'the API was just simplified for developers'. It is a fundamental GC design consequence, not a convenience choice. Another trap: assuming WeakMap values are also weak — they are not; values are strongly held, they simply die together with their key. Candidates who explain the ephemeron key/value asymmetry show real understanding.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.