Sub topic 1
Why JavaScript Has Keyed Collections
For years, JavaScript developers used plain objects as dictionaries and arrays as lists of unique items. Both approaches work until they don't. ES2015 added four built-in keyed collections to cover the gaps: Map, Set, WeakMap, and WeakSet. This chapter explains the problems they solve and how they relate to each other. Later chapters cover each one in detail.
Prerequisites
This tutorial assumes you are comfortable with:
- JavaScript variables, functions, and arrow functions
- Objects and arrays, including property access
for...ofloops and the spread operator- Basic ES classes
- The difference between primitive values and object references
The problem with objects as dictionaries
A plain object stores keys only as strings or symbols. Any other key gets converted to a string, and that conversion can quietly lose data:
const userA = { name: 'Ada' };
const userB = { name: 'Grace' };
const scores = {};
scores[userA] = 10;
scores[userB] = 20;
console.log(Object.keys(scores)); // ['[object Object]']
console.log(scores[userA]); // 20, overwritten by userBBoth user objects turn into the string '[object Object]', so they share one slot. Objects also inherit properties from Object.prototype, so a key such as 'constructor' can collide with something you never stored. On top of that, objects have no built-in size property and no direct iteration protocol.
Arrays have their own weakness when you want unique values. Checking for duplicates with includes scans the whole array, and nothing stops a duplicate from being pushed.
What keyed collections add
A keyed collection is a data structure that indexes its entries by key. In a Set, each value is its own key. All four built-ins share these traits:
- Any value can be a key, including objects, and keys are never converted to strings.
- Lookups by key are fast. The specification requires sublinear average access time.
- There are no inherited keys to collide with.
They split along two axes: whether they store key-value pairs or single values, and whether they hold their keys strongly or weakly.
| Collection | Stores | Key types | Iterable | Has `size` |
|---|---|---|---|---|
| `Map` | key → value pairs | any value | yes | yes |
| `Set` | unique values | any value | yes | yes |
| `WeakMap` | key → value pairs | objects and non-registered symbols | no | no |
| `WeakSet` | unique values | objects and non-registered symbols | no | no |
A strong reference keeps its target alive. As long as a Map holds an object as a key, the garbage collector cannot reclaim that object. A weak reference does not keep its target alive. Once nothing else references the object, the engine may remove it and its entry. That freedom costs you iteration and size, which the weak-collection chapters explain.
Common misconceptions
- "Map is just a fancier object." The differences in key types, ordering guarantees, and size tracking change which one you should pick. A later chapter compares them directly.
- "Weak collections are faster." They exist to manage memory, not to speed things up.
- "A Set compares objects by content." Two different objects with identical properties are still two separate entries.
Exercise
You use a plain object to count how many times each DOM element was clicked, writing counts[element] += 1. Why does this give wrong results, and which collection would you use instead if you also need to list every element that was clicked?
Show answer
Object keys are coerced to strings, so every element becomes a string such as '[object HTMLDivElement]' and many elements end up sharing one counter. Use a Map, because it accepts the elements themselves as keys and lets you iterate over them. A WeakMap would also keep the counts apart, but you cannot iterate over it.