Skip to solution
mediumFrontend

Why would you choose a WeakSet over a Set to track a group of DOM elements, and what happens to its entries once those elements are removed?

352 views
01

Understand the problem

Question presented to candidate: "You want to mark which DOM buttons have already been processed, so you never double-process one. If you use a regular Set to track them, and a button is later removed from the page, what happens to it in memory? What would you do differently?"

What a strong answer should cover:

  • 📌 Interview term: WeakSet — a Set-like collection holding only weak references to the objects (only real objects — never primitives) added to it — meaning the presence of an object in a WeakSet genuinely does NOT, by itself, prevent that object from being garbage collected once nothing else references it.
  • 📌 Interview term: the real, direct answer to the prompt — verified directly, with a real jsdom-backed DOM: a regular Set holds a real, STRONG reference to every element added to it — even after a button is genuinely removed from the DOM tree, the Set alone would keep it alive in memory forever, a real, classic memory-leak pattern. A WeakSet holding the same reference genuinely does NOT prevent collection.
  • 📌 Interview term: the real trade-off this buys — a WeakSet is genuinely NOT iterable, has NO .size, and NO .forEach() — verified directly (typeof processedButtons.size is genuinely "undefined") — because if you could list its contents, that list itself would need to hold real, live references, defeating the entire weak-reference purpose.
  • 📌 Interview term: primitive rejection — verified directly: calling .add("a string") on a real WeakSet genuinely THROWS a real TypeError — only real objects (which are genuinely garbage-collectible) are allowed, since a primitive value cannot meaningfully be "weakly referenced" the way an object can.
  • A precise answer names the practical fix for the prompt's own scenario: a WeakSet tracking processed DOM elements genuinely self-cleans as elements are removed and eventually collected — no memory leak, and no need to ever manually .delete() an element when it is torn down.

Clarifying questions expected:

  • None — this is a definitional/practical question; directly answering the prompt's own memory-lifecycle question with real, verified proof is the strong signal.

Code / implementation expected: Yes — a real, jsdom-backed demonstration of tracking DOM buttons with a WeakSet, plus real proof of the primitive-rejection and no-iteration constraints.

weaksettrickyreal-world
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The DOM-tracking claims below were verified directly against a real jsdom document with r

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal, runnable proof using genuine DOM elements: WeakSet correctly prevents duplicate processing, rejects primitives, and holds no strong reference — a regular Set does
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 98 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track