JavaScript WeakSet: Your Secret Guest List
Think of a party guest list where only special guests can enter. But here’s the twist:
- The names on the list are not written in ink, they are written in disappearing ink.
- If a guest leaves the party and no one remembers them anymore, their name vanishes automatically from the list.
That’s what a WeakSet is in JavaScript.
What is a WeakSet?
A WeakSet is like a normal Set, but with some special rules:
- It only stores objects (not numbers, strings, or other primitive values).
- If an object is no longer referenced anywhere in the program, it will be garbage collected and automatically removed from the WeakSet.
- It doesn’t keep a permanent record—you can’t list out all its values.
How is it different from Set?
- Set can store anything (numbers, strings, objects) and you can loop through it.
- WeakSet only stores objects, and you cannot loop through its contents.
Example
let weakGuests = new WeakSet();
let guest1 = { name: "Alice" };
let guest2 = { name: "Bob" };
weakGuests.add(guest1);
weakGuests.add(guest2);
console.log(weakGuests.has(guest1)); // true
guest1 = null; // Alice leaves the party
// Automatically removed from WeakSet!
Here:
guest1
andguest2
are objects added to the WeakSet.- When
guest1
is set tonull
, WeakSet forgets about Alice automatically.
Methods of WeakSet
.add(object)
→ adds an object.has(object)
→ checks if object is in the set.delete(object)
→ removes an object
Real-Life Analogy
WeakSet is like a VIP invisible guest list:
- Only people (objects) can enter.
- If someone leaves and no one remembers them, their name disappears.
- You can check if someone is on the list, but you can’t see the full list.
In Short
- WeakSet = special set for objects only.
- Objects disappear when no longer needed.
- Good for memory-friendly tracking of objects.
Review Questions
- A WeakSet is similar to a Set, but it only stores __________.
- If an object in a WeakSet is no longer referenced, it is automatically __________ collected.
- Unlike a normal Set, WeakSet cannot store __________ values.
- The method used to add an object to a WeakSet is __________.
- The method used to check if an object exists in a WeakSet is __________.
- The method used to remove an object from a WeakSet is __________.
- You cannot __________ through the contents of a WeakSet.
- WeakSet helps prevent __________ leaks by automatically removing unreferenced objects.
- A real-life analogy for WeakSet is a __________ list with disappearing ink.
- Normal Set can store numbers, strings, and objects, but WeakSet can only store __________.