JavaScript provides built-in collection types for storing groups of values:
MapSetWeakMapWeakSet
These offer better performance, flexibility, and functionality than regular objects and arrays for certain use cases.
A Map is a collection of key-value pairs, similar to objects, but with more features.
- Keys can be of any data type (including objects, functions, etc.)
- Maintains the insertion order
- Can easily get the size
const myMap = new Map();
myMap.set("name", "Varun");
myMap.set(1, "One");
myMap.set(true, "Boolean");
console.log(myMap.get("name")); // "Varun"
console.log(myMap.size); // 3for (let [key, value] of myMap) {
console.log(key, value);
}A Set is a collection of unique values.
- Stores only unique values
- Can hold values of any type
- Maintains insertion order
const mySet = new Set();
mySet.add(1);
mySet.add(2);
mySet.add(2); // ignored, already exists
console.log(mySet.has(1)); // true
console.log(mySet.size); // 2mySet.forEach(value => console.log(value));A WeakMap is like a Map, but:
- Keys must be objects
- Keys are weakly referenced (not prevented from garbage collection)
- Can't iterate or check size
- Great for storing private data tied to DOM elements or objects
- Helps avoid memory leaks
const wm = new WeakMap();
let obj = {};
wm.set(obj, "private value");
console.log(wm.get(obj)); // "private value"
obj = null; // Now the key-value pair is eligible for garbage collection
⚠️ WeakMapmethods:set,get,has,delete
A WeakSet is like a Set, but:
- Only stores objects
- Objects are held weakly
- Not iterable
- Cannot check size or iterate
- Useful for managing object references without memory leaks
const ws = new WeakSet();
let user = { name: "Varun" };
ws.add(user);
console.log(ws.has(user)); // true
user = null; // Automatically removed from the WeakSet| Feature | Map | Set | WeakMap | WeakSet |
|---|---|---|---|---|
| Keys/Values | key-value | values | key-value | values |
| Key type | Any | Any | Object only | Object only |
| Duplicates | No | No | No | No |
| Order | Preserved | Preserved | N/A | N/A |
| Iterable | ✅ | ✅ | ❌ | ❌ |
| Garbage Col. | ❌ | ❌ | ✅ | ✅ |
| Size Access | ✅ | ✅ | ❌ | ❌ |
| Use Case | Best Collection |
|---|---|
| Unique values | Set |
| Key-value store with non-string keys | Map |
| Memory-safe object-key storage | WeakMap |
| Track object references | WeakSet |
- Store usernames uniquely →
Set - Cache user metadata →
Map - Track element-specific data safely →
WeakMap - Track whether objects were seen →
WeakSet
- Use
Map/Setwhen you need iteration and full data control - Use
WeakMap/WeakSetwhen dealing with temporary object references and memory-sensitive operations
Happy Coding! 🚀