Skip to the solution
MediumFrontend6 min read

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

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.

  • weakset
  • tricky
  • real-world

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, runnable proof using genuine DOM elements: WeakSet correctly prevents duplicate processing, rejects primitives, and holds no strong reference β€” a regular Set does

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 98 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

What is `Array.fromAsync()` and how does it handle async iterables differently from `Array.from()`?

Continue