Why Maps and Sets?
Prior to ES6, we only had Objects and Arrays to store collections of data. Maps and Sets provide more specialized, high-performance data structures for specific use cases.
1. Maps (Key-Value)
A Map is like an Object, but with one major difference: Keys can be any value, including objects, functions, or numbers! In a regular Object, keys are converted into strings.
javascript
Map vs. Object
| Feature | Map | Object |
|---|---|---|
| Key Types | Any (Object, Function, Number) | String or Symbol |
| Order | Preserves insertion order! | Usually preserves order! |
| Length | .size | Must use Object.keys(). |
| Iterate | Built-in (for...of) | Must use Object.keys/entries(). |
2. Sets (Unique Collections)
A Set is an ordered collection of unique values. Duplicates are automatically removed. It's much faster than an Array for verifying membership (
.has()).javascript
Use Case: Removing Duplicates from an Array
javascript
3. WeakMaps & WeakSets
These are specialized versions that do not prevent garbage collection of their keys. If a key is an object and no other reference to it exists, it will be automatically removed from the Map/Set.
javascript
Note: WeakMaps only allow objects as keys, and you cannot iterate over them.
4. WeakRef (ES2021+)
A WeakRef is a weak reference to an object. It allows you to refer to an object without keeping it "alive" for the garbage collector.
Summary
| Feature | Description |
|---|---|
| Map | Dictionary where keys can be anything |
| Set | Collection where all items are unique |
| WeakMap | Keys are objects; automatically garbage collected |
| WeakSet | Items are objects; automatically garbage collected |
| Best For | Metadata storage, unique lists, performance |
| Set.has() | Very fast (O(1) complexity!) |
| Map.size | Quick check for item count |
| Clear() | All Maps/Sets have a .clear() method |
| Memory Leak | WeakMap prevents leaks by allowing GC |
| Key Identity | NaN === NaN in Maps (unlike in Math!) |
| For...Of | Native iteration support |
| Entry List | Convert Map back to array with [...map.entries()] |
| Prototype | Maps don't have default keys (Objects have toString, etc.) |
| Collision | No risk of key collision with inherited properties |
| Performance | Better for frequent additions and removals |
| Key Equality | Uses SameValueZero algorithm |
| WeakRef.deref | Access the object inside the weak reference |
| Iteration | Only Map and Set are iterable |