Compile Ready
Module 11 · Advanced JavaScript

WeakSet

Advanced11m read18m practice29m total
WeakSetGarbage CollectionVisited SetObject Identity

Introduction

A WeakSet stores objects weakly, representing membership without preventing garbage collection. It answers one question: has this object been seen or marked?

It is ideal for visited-object tracking, cycle detection, and hidden membership flags where enumeration is unnecessary or harmful.

Why This Matters

WeakSet appears in deep-clone, graph traversal, serializer, and cycle-detection interviews. It also tests a subtle memory concept: tracking objects should not always keep those objects alive.

Theory

Object membership only

Like WeakMap keys, WeakSet values must be objects. Adding a primitive throws TypeError. Membership is based on object identity, not structural equality.

Weak references

If an object in a WeakSet becomes unreachable elsewhere, it can be garbage-collected. The WeakSet does not keep it alive.

No enumeration

WeakSet has add, has, and delete, but no size or iteration. This prevents programs from observing garbage collection timing.

Common uses

Track visited objects in recursive algorithms, mark objects as initialized or validated, detect cycles without leaks, and hide membership without mutating the object. Use Set when you need primitives, enumeration, or counts.

Visual Diagrams

WeakSet visited tracking
visit object A
   | add A to WeakSet
visit child B
   | add B to WeakSet
visit A again
   | WeakSet has A -> cycle detected

WeakSet is perfect when the only stored information is seen or not seen.

Code Examples

Detect cycles during traversal

The WeakSet marks objects already on the traversal path.

Loading…

Playground

Loading editor…
Console

Press Run to execute the code and see output here.

Output Prediction

Predict the output #1

javascript
1var seen = new WeakSet();
2var a = {};
3var b = {};
4
5seen.add(a);
6
7console.log(seen.has(a));
8console.log(seen.has(b));
9
10try {
11 seen.add(1);
12} catch (error) {
13 console.log(error.name);
14}

Coding Exercises

Detect cycles in an object graph

Medium

Implement hasCycle(value) for arrays and objects. It should return true if traversal reaches an object already seen, and it should use WeakSet to avoid leaking visited objects.

Interview Questions

1How is WeakSet different from Set?

WeakSet stores only objects, holds them weakly, and cannot be iterated or sized. Set can store primitives, is iterable, has size, and strongly retains its values.

Asked at:AmazonGoogleMeta

Follow-ups

  • Why does WeakSet not expose `size`?
  • Where would you use WeakSet in deep clone?
2Why is WeakSet useful for cycle detection?

Cycle detection only needs membership, not enumeration. WeakSet can mark visited objects without mutating them and without keeping them alive longer than necessary.

Quiz

1. Which value can be added to a WeakSet?

Summary

  • WeakSet stores object membership weakly.
  • It supports `add`, `has`, and `delete`, but not iteration or `size`.
  • Use it for visited tracking, cycle detection, and hidden object marks.
  • Use Set when you need primitives, enumeration, or counts.

Cheat Sheet

API: add, has, delete.

Members: objects only.

No enumeration: no size, no iteration.

Use cases: visited objects, cycle detection, hidden marks.

Memory: membership does not keep objects alive.