JavaScript Set and WeakSet
In JavaScript, both Set and WeakSet are objects used to store unique values. While they share some similarities, they also have key differences that make them useful in different scenarios.
A Set is a collection of unique values. It allows you to store values of any type, including objects, and automatically removes duplicates. Sets are useful when you need to ensure that no duplicate values are present in a collection.
You can create a Set using the new Set()
constructor:
const mySet = new Set();
You can also initialize a Set with values when creating it:
const numbers = new Set([1, 2, 3, 4, 5]);
In this example, the Set
contains five unique values.
Here are the key features of the Set object:
You can add values to a Set using the add()
method.
const mySet = new Set();
mySet.add(1);
mySet.add(2);
mySet.add(3);
console.log(mySet); // Output: Set { 1, 2, 3 }
If you try to add a duplicate value, it won’t be added:
mySet.add(3); // No effect
console.log(mySet); // Output: Set { 1, 2, 3 }
You can check if a Set contains a value using the has()
method.
console.log(mySet.has(2)); // Output: true
console.log(mySet.has(4)); // Output: false
To remove a specific value from a Set, use the delete()
method.
mySet.delete(2);
console.log(mySet); // Output: Set { 1, 3 }
To remove all values from a Set, use the clear()
method.
mySet.clear();
console.log(mySet); // Output: Set {}
You can iterate over the values of a Set using different methods:
forEach()
mySet.forEach(value => {
console.log(value);
});
for...of
Loop
for (let value of mySet) {
console.log(value);
}
values()
Method
const setValues = mySet.values();
console.log([...setValues]); // Output: [1, 3]
A WeakSet is similar to a Set, but with a few important differences. A WeakSet only stores objects as its values, and the values are weakly referenced. This means that if there are no other references to an object in a WeakSet, it can be garbage collected, making WeakSet more memory efficient for certain use cases.
You can create a WeakSet using the new WeakSet()
constructor:
const myWeakSet = new WeakSet();
You can also initialize a WeakSet with an array of objects:
const obj1 = { name: 'Alice' };
const obj2 = { name: 'Bob' };
const myWeakSet = new WeakSet([obj1, obj2]);
clear()
Method: A WeakSet does not have a clear()
method because its items are automatically garbage collected when there are no references to them.You can add objects to a WeakSet using the add()
method:
const myWeakSet = new WeakSet();
const obj1 = { name: 'John' };
const obj2 = { name: 'Jane' };
myWeakSet.add(obj1);
myWeakSet.add(obj2);
To check if an object exists in a WeakSet, use the has()
method:
console.log(myWeakSet.has(obj1)); // Output: true
console.log(myWeakSet.has(obj2)); // Output: true
To remove an object from a WeakSet, use the delete()
method:
myWeakSet.delete(obj1);
console.log(myWeakSet.has(obj1)); // Output: false
Feature | Set | WeakSet |
---|---|---|
Key Type | Any type (primitives, objects, functions) | Only objects |
Garbage Collection | Does not support garbage collection | Weak references; objects are garbage-collected if no other references exist |
Iteration | Supports iteration (forEach, for...of) | No iteration available |
Methods | clear() , size , add() , has() , delete() |
add() , has() , delete() |
Use Case | Storing any unique values, with iteration | Memory-efficient storage for objects that might be garbage collected |