Skip to the solution
HardFrontend6 min read

What are WeakMap and WeakSet?

211 views

The problem

Question presented to candidate: "Why would you reach for a WeakMap instead of a regular Map to attach some private metadata to a set of objects? What real constraints does WeakMap have that a regular Map doesn't?"

What a strong answer should cover:

  • πŸ“Œ Interview term: WeakMap/WeakSet β€” collections that hold their keys (WeakMap) or values (WeakSet) with weak references, meaning the JavaScript engine's garbage collector can reclaim that object's memory once nothing else in the program references it, even though the WeakMap/WeakSet itself still technically "contains" it.
  • πŸ“Œ Interview term: object-only keys/values β€” verified directly: a WeakMap/WeakSet genuinely throws a real TypeError ("Invalid value used as weak map key" / "...weak set") for a string, number, or registered symbol (Symbol.for(...)) β€” only objects, and plain (non-registered) Symbols, are accepted.
  • πŸ“Œ Interview term: no enumeration β€” verified directly: neither has a real .size property, a real Symbol.iterator, nor a real .forEach β€” they are genuinely not iterable and cannot be inspected as a whole, only queried for one specific key/value at a time (.get/.has).
  • A precise answer names the real, practical use case this constraint enables: attaching private, per-object metadata (like a cache entry, or "has this been processed") that automatically disappears when the original object is garbage collected, without the WeakMap itself artificially keeping that object alive β€” verified directly with a real "process each object only once" pattern.
  • A precise answer names the direct contrast with a regular Map: verified directly, a regular Map genuinely DOES have .size and genuinely IS iterable β€” the exact features WeakMap deliberately omits specifically to make weak referencing and non-enumeration reliable.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; naming the object-only constraint AND the no-enumeration constraint together, with the real reason both exist, is the strong signal.

Code / implementation expected: Yes β€” showing the real TypeError for a primitive key, plus the missing .size/iterability, demonstrates genuine understanding beyond "it's like Map but weak."

  • weakmap

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 proof of WeakMap/WeakSet's two constraints: object-only keys and no enumeration, plus a real process-once pattern

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 160 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 nextHard

How do you implement a simple Promise library from scratch?

Continue