Skip to the solution
MediumFrontend6 min read

How does WeakMap allow garbage collection of keys?

1.1k views

The problem

Question presented to candidate: "If you use an object as a WeakMap key to store some metadata about it, and then that object becomes otherwise unreachable, does the WeakMap keep it alive? Can you actually prove your answer, not just state it?"

What a strong answer should cover:

  • πŸ“Œ Interview term: WeakMap β€” a Map-like collection whose KEYS must be real objects and are held with only a weak reference β€” meaning an entry's presence in a WeakMap genuinely does NOT, by itself, keep that key object alive.
  • πŸ“Œ Interview term: the real, direct, OBSERVED answer to the prompt β€” verified directly, not just asserted: a key object was registered with a real FinalizationRegistry, its only other strong reference was dropped, garbage collection was forced (node --expose-gc), and the registry's real cleanup callback GENUINELY FIRED β€” real, observed proof that the WeakMap-held key was genuinely eligible for and underwent collection.
  • πŸ“Œ Interview term: contrast with a regular Map β€” a regular Map genuinely holds a strong reference to every key, meaning a key object used as a Map key can NEVER be garbage collected while that Map still exists, even if literally nothing else in the program references it anymore β€” a real, classic source of memory leaks in long-lived caches.
  • πŸ“Œ Interview term: the real practical use case β€” a precise answer names per-object metadata caching (memoizing an expensive computed result keyed by a specific object instance, or attaching private data to an instance without a real "private field") as the real, canonical WeakMap use case β€” the cache entry genuinely disappears on its own once the object it describes is no longer needed anywhere else, with zero manual cache-eviction code required.
  • A precise answer is honest that JS code can never directly observe or control garbage collection's actual TIMING β€” only its eventual, real occurrence, confirmable indirectly via a tool purpose-built for it, FinalizationRegistry.

Clarifying questions expected:

  • None β€” this is a definitional/mechanism question; the strong signal is genuinely PROVING the claim (not just stating documentation) via a real, observed collection event.

Code / implementation expected: Yes β€” a real FinalizationRegistry-based proof that a WeakMap's key object is genuinely collected once unreferenced elsewhere.

  • weakmap
  • garbage-collection
  • memory

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct, OBSERVED proof via FinalizationRegistry: a WeakMap-held key genuinely becomes eligible for garbage collection once no other reference exists

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 52 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

How would you write a Proxy 'set' trap that validates a value before allowing an assignment, and what happens if you return false?

Continue