1. Sets: Unordered Collections of Unique Values
Sets are collections of values where each value must be unique. They provide methods for adding, deleting, and checking the existence of elements.
// Creating a Set
const mySet = new Set([1, 2, 3, 1, 2]); // Duplicates are automatically removed
// Adding elements to a Set
mySet.add(4).add(5);
// Deleting elements from a Set
mySet.delete(2);
// Checking the existence of an element
console.log(mySet.has(3)); // Output: true
// Iterating over a Set
mySet.forEach(value => console.log(value));
// Output:
// 1
// 3
// 4
// 52. Maps: Key-Value Pairs for Efficient Data Storage
Maps are collections of key-value pairs, allowing efficient data retrieval based on keys.
// Creating a Map
const myMap = new Map();
// Adding key-value pairs to a Map
myMap.set("name", "Alice");
myMap.set("age", 30);
// Retrieving values from a Map
console.log(myMap.get("name")); // Output: Alice
// Checking the existence of a key
console.log(myMap.has("gender")); // Output: false
// Deleting a key-value pair from a Map
myMap.delete("age");
// Iterating over a Map
myMap.forEach((value, key) => console.log(`${key}: ${value}`));
// Output:
// name: Alice3. Sets vs. Arrays
Sets offer distinct advantages over arrays for certain use cases:
// Using a Set to remove duplicates from an array
const arrayWithDuplicates = [1, 2, 3, 1, 2];
const uniqueValues = [...new Set(arrayWithDuplicates)];
console.log(uniqueValues); // Output: [1, 2, 3]Is a Set faster than an Array?
The performance characteristics of sets and arrays depend on the specific operations you’re performing. Here are some key points to consider:
- Uniqueness:
- Sets are designed for ensuring uniqueness. When you need to check whether an item exists in a collection and you don’t care about duplicates, an array might be faster.
- If uniqueness is crucial, using a set ensures faster membership tests since it’s optimized for this purpose.
- Iteration:
- Arrays are ordered, and iterating over them using a
forloop can be faster than iterating over a set using theforEachmethod. - If order doesn’t matter and you need to iterate over unique elements, a set might be a better choice.
- Arrays are ordered, and iterating over them using a
- Insertion and Deletion:
- Sets excel at adding and deleting elements, especially when uniqueness is a concern.
- Arrays might be faster for some insertion and deletion operations, especially if you’re working with a small number of elements.
- Memory Usage:
- Sets generally use less memory compared to arrays, especially when you have a large number of unique elements.
- Search:
- Searching for an element in an array using methods like
indexOfcan be faster than searching in a set if the position/index of the element is known. - Sets provide faster lookup times when you don’t know the position/index and just need to check for existence.
- Searching for an element in an array using methods like
4. WeakSet
Certainly! WeakSet is a specialized collection in JavaScript that allows you to store weakly held object references. The primary characteristic of a WeakSet is that it allows the garbage collector to reclaim the objects it references when those objects are no longer used elsewhere in your program. Here are some key points about WeakSet:
Weak References:
- Unlike a regular
Set, aWeakSetcan only contain objects, and these objects are held by weak references. - Weak references allow the garbage collector to automatically reclaim an object if there are no other strong references to it.
Automatic Garbage Collection:
- When an object is added to a
WeakSet, it doesn’t prevent the object from being garbage-collected if there are no other references to it outside of theWeakSet. - This behavior is useful when you want to associate some data with objects, but you don’t want to prevent those objects from being garbage-collected when they are no longer needed.
No Iteration:
- Unlike
Set,WeakSetdoes not have methods for iteration such asforEach,keys,values, orentries. This is because the contents of aWeakSetare not enumerable.
Methods:
WeakSethas three main methods:add(value),has(value), anddelete(value).- It lacks methods for iterating over its contents since iteration is not allowed due to the weak reference nature.
Use Cases:
WeakSetis often used in scenarios where you want to associate additional data or metadata with specific objects but don’t want to prevent those objects from being garbage-collected when they are no longer used elsewhere.
Here’s a basic example:
let object1 = { key: "value" };
let object2 = { key: "value" };
const weakSet = new WeakSet([object1, object2]);
console.log(weakSet.has(object1)); // Output: true
// If there are no other references to object1 and object2, they can be garbage-collected
object1 = null;
object2 = null;In this example, if there are no other references to object1 and object2, they may be garbage-collected, and the WeakSet will automatically remove them from its collection.
Keep in mind that the use of WeakSet is more specialized, and it’s particularly helpful in scenarios where automatic garbage collection of associated objects is desired.
4. WeakMap
WeakMap is another specialized data structure in JavaScript, similar to WeakSet, that allows you to create a collection of key-value pairs where the keys are weakly referenced. Here are some key points about WeakMap:
Key Weak References:
- Just like
WeakSet,WeakMapallows the keys to be weakly referenced. This means that if there are no other strong references to a key, it can be garbage-collected.
Object-Only Keys:
- In a
WeakMap, keys can only be objects. This is different from regularMapwhere keys can be any data type, including primitives.
Automatic Garbage Collection:
- If an object used as a key in a
WeakMapis no longer referenced anywhere else in your program, it becomes eligible for garbage collection, and the corresponding key-value pair is automatically removed from theWeakMap.
No Iteration:
- Similar to
WeakSet,WeakMapdoes not have methods for direct iteration. This is because the contents of aWeakMapare not enumerable.
Methods:
WeakMapprovides methods likeset(key, value),get(key),has(key), anddelete(key).
Use Cases:
WeakMapis commonly used when you want to associate additional data with objects but without preventing those objects from being garbage-collected.
Here’s a simple example:
let key1 = { id: 1 };
let key2 = { id: 2 };
const weakMap = new WeakMap();
// Setting key-value pairs
weakMap.set(key1, "Value associated with key1");
weakMap.set(key2, "Value associated with key2");
console.log(weakMap.get(key1)); // Output: Value associated with key1
// If there are no other references to key1, it can be garbage-collected, and the entry is automatically removed
key1 = null;
console.log(weakMap.has(key1)); // Output: falseIn this example, if there are no other references to key1, it becomes eligible for garbage collection, and the corresponding entry in the WeakMap is automatically removed.
Use WeakMap when you need to associate private or additional data with objects and you don’t want to interfere with the garbage collection of those objects.
Conclusion
Sets and Maps offer powerful alternatives to arrays and objects in JavaScript, providing distinct functionalities for different scenarios. Whether you need to ensure uniqueness, efficient key-based data retrieval, or garbage collection-friendly structures, Sets and Maps have got you covered.

Leave a Reply