Set is a special type collection, like an array where each value may occur only once Operations on sets are faster than arrays & objects Following examples are taken mostly from here Declaration new Set([iterable])
let set = new Set([1,1,2,2,3,3]) // Set(3) {0:1, 1:2, 2:3}
Add
const set1 = new Set(); // Set(0) {}
set1.add(1);
set1.add(2);
set1.add(1); // Set(2) { 1, 2 }
Set out of Set
const set3 = new Set([1,2,3]);
const set4 = new Set(set3);
console.log(set3); // { 1, 2, 3 }
console.log(set4); // { 1, 2, 3 }
set4.add(666);
console.log(set3); // { 1, 2, 3 }
console.log(set4); // { 1, 2, 3, 666 }
Clear
const set5 = new Set([3,2,1,1,2]);
console.log(set5); // Set(3) { 3, 2, 1 }
set5.clear();
console.log(set5); // Set(0) {}
set5.add(1);
console.log(set5) // Set(1) { 1 }
Delete
// returns true if value existed at the moment of the call, otherwise false.
const set6 = new Set([3,2,1,1,2]);
console.log(set6); // { 3, 2, 1 }
set6.delete(1); // true
console.log(set6); // { 3, 2 }
Set to array
const set7 = new Set([1,2,3])
console.log(Array.from(set7)) // [1,2,3]
console.log([...set7]) // [1,2,3]
Get it doesn't have index-based access there is no dedicated method to get a value Set is designed for uniqueness rather than indexed access Check if value exists
const mySet = new Set([10, 20, 30]);
console.log(mySet.has(20)); // true
via to array
const mySet = new Set([10, 20, 30]);
const arr = [...mySet]; // Convert to an array
console.log(arr[1]); // 20
const mySet = new Set([10, 20, 30]);
console.log(Array.from(mySet)[1]); // 20
via for...of loop
const mySet = new Set([10, 20, 30]);
for (let value of mySet) {
if (value === 20) {
console.log(value); // 20
break;
}
}
Size
const set1 = new Set([10,20,100,200]);
set1.size; // 4
Has
const set2 = new Set([10,20,100,200]);
console.log("10:", set2.has(10)); // true
console.log("100:", set2.has(100)); // true
console.log("1000:", set2.has(1000)); // false
entries keys values
// set.keys() // returns an iterable object for values, because there are no keys in Set, exists for compatibility with Map.
// set.values() // returns an iterable object for values
// set.entries() // returns an iterable object for entries [value, value], exists for compatibility with Map.
const set3 = new Set(["Jane", "John", "Jim", "Jill"]);
console.log('Entries:', set3.entries());
/*
[Set Entries] {
[ "Jane", "Jane" ],
[ "John", "John" ],
[ "Jim", "Jim" ],
[ "Jill", "Jill" ]
}
*/
console.log('Keys:', set3.keys()); // [Set Iterator] { "Jane", "John", "Jim", "Jill" }
console.log('Values:', set3.values()); // [Set Iterator] { "Jane", "John", "Jim", "Jill" }
Iteration
const set4 = new Set(["Jane", "John", "Jim", "Jill"]);
set4.forEach((value) => console.log(value)); // Jane --> John --> Jim --> Jill
for (const value of set4) {
console.log(value); // Jane --> John --> Jim --> Jill
}
Performance
const arr = new Array(300000).fill(0).map((_, index) => index);
const startTime = performance.now()
for (let i = 0; i < 300000; i++) {
arr.includes(i);
}
const endTime = performance.now()
console.log(endTime - startTime) // 4779 ms
const set = new Set(new Array(300000).fill(0).map((_, index) => index));
const startTime = performance.now()
for (let i = 0; i < 300000; i++) {
set.has(i);
}
const endTime = performance.now()
console.log(endTime - startTime) // 1.5 ms
WeakSet allows to store only objects removes them once they become inaccessible main advantages are that they have weak reference they can easily be removed by garbage collector disadvantages are not having support for clear, size, keys, values… An object exists in the set while it is reachable from somewhere else Like Set, it supports add, has and delete, but not size, keys() and no iterations.
let weakSet = new WeakSet()
let john = { name: "John" }
let pete = { name: "Pete" }
let mary = { name: "Mary" }
weakSet.add(john)
weakSet.add(pete)
weakSet.add(john)
// weakSet has 2 users now
// check if John visited?
alert(weakSet.has(john)) // true
alert(weakSet.has(mary)) // false
john = null
// weakSet will be cleaned automatically
union
const setA = new Set([1,2,3,4,5,6]);
const setB = new Set([2,3,10,50,60]);
console.log('Union:', setA.union(setB));
console.log('Intersection:', setA.intersection(setB));
console.log('Difference:', setA.difference(setB));
console.log('Symmetric Difference:', setA.symmetricDifference(setB));
union
const setA = new Set([1,2,3,4,5,6]);
const setB = new Set([2,3,10,50,60]);
console.log('Union:', setA.union(setB));
// Set(9) { 1, 2, 3, 4, 5, 6, 10, 50, 60 }
intersection
const setA = new Set([1,2,3,4,5,6]);
const setB = new Set([2,3,10,50,60]);
console.log('Intersection:', setA.intersection(setB));
// Set(2) { 2, 3 }
difference
const setA = new Set([1,2,3,4,5,6]);
const setB = new Set([2,3,10,50,60]);
console.log('Difference:', setA.difference(setB));
// Set(4) { 1, 4, 5, 6 }
symmetricDifference
const setA = new Set([1,2,3,4,5,6]);
const setB = new Set([2,3,10,50,60]);
console.log('Symmetric Difference:', setA.symmetricDifference(setB));
// Set(7) { 1, 4, 5, 6, 10, 50, 60 }
isSupersetOf
const names1 = new Set(['John', 'Jane', 'Alice', 'Bob']);
const names2 = new Set(['John', 'Jane']);
console.log(names1.isSupersetOf(names2)) // true
isSubsetOf
const names1 = new Set(['John', 'Jane', 'Alice', 'Bob']);
const names2 = new Set(['John', 'Jane']);
console.log({names1.isSubsetOf(names2)); // false
console.log({names2.isSubsetOf(names1)); // true
isDisjointFrom
const names1 = new Set(['John', 'Jane', 'Alice', 'Bob']);
const names2 = new Set(['John', 'Jane']);
const names3 = new Set(['Zoe']);
console.log(names1.isDisjointFrom(names2)); // false
console.log(names1.isDisjointFrom(names3)); // true