DEV Community

Cover image for WeakSet in JS?
_Khojiakbar_
_Khojiakbar_

Posted on

WeakSet in JS?

A WeakSet in JavaScript is a special kind of set where the objects inside it have "weak" references. This means that if there are no other references to an object stored in the WeakSet, the object can be garbage collected. Unlike a regular Set, WeakSet only accepts objects as elements, and those objects are held weakly. This makes WeakSet useful for cases where you want to track objects but don't want to prevent them from being garbage collected if they're no longer needed elsewhere.

Characteristics of WeakSet:

1 Only Objects: A WeakSet can only contain objects, not primitive values like numbers or strings.

2 Weak References: The references to objects in a WeakSet are weak, meaning that if there is no other reference to an object, it can be garbage collected.

3 No Size Property: You can't get the size of a WeakSet because it doesn't expose the number of its elements.

4. No Iteration: You can't iterate over a WeakSet, as it doesn't have methods like forEach or iterators like a Set.

Basic Usage:

let weakset = new WeakSet();

let obj1 = {name: "object1"};
let obj2 = {name: "object2"};

weakset.add(obj1);
weakset.add(obj2);

console.log(weakset.has(obj1)); // true
console.log(weakset.has(obj2)); // true

obj1 = null; // obj1 is eligible for garbage collection

console.log(weakset.has(obj1)); // false
Enter fullscreen mode Exit fullscreen mode

Funny Sample:

Let's imagine a scenario where WeakSet is like a secret club for spies. This club is very secretive, and if a spy is no longer active, they just vanish without a trace. The club never keeps a count of its members, and you can't get a list of who is currently in the club. You can only ask if a specific spy is in the club.

// The Secret Spy Club
class Spy {
    constructor(name) {
        this.name = name;
    }
    introduce() {
        console.log(`Hi, I am Agent ${this.name}`);
    }
}

let spy1 = new Spy("007");
let spy2 = new Spy("008");

let spyClub = new WeakSet();

// Adding spies to the secret club
spyClub.add(spy1);
spyClub.add(spy2);

console.log(spyClub.has(spy1)); // true
console.log(spyClub.has(spy2)); // true

spy1.introduce(); // Hi, I am Agent 007
spy2.introduce(); // Hi, I am Agent 008

// Spy 007 finishes his mission and disappears
spy1 = null; // Now Agent 007 is no longer in the club and is eligible for garbage collection

// Let's see if spies are still in the club
console.log(spyClub.has(spy1)); // false, because Agent 007 is no longer referenced
console.log(spyClub.has(spy2)); // true, because Agent 008 is still active

// Agent 008 finishes his mission too
spy2 = null; // Now Agent 008 is also eligible for garbage collection

// Checking club members again
console.log(spyClub.has(spy2)); // false, no active spies left
Enter fullscreen mode Exit fullscreen mode

In this funny example, the WeakSet is the secret spy club, and the spies are objects. When spies (objects) complete their missions and there are no other references to them, they disappear (get garbage collected) without any trace, just like how objects are removed from a WeakSet when they are no longer referenced elsewhere in the code.

Top comments (0)