Suggest an editImprove this articleRefine the answer for “WeakMap vs Map”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**Both structures store "key to value" pairs, but a `Map` holds its keys with strong references while a `WeakMap` holds them weakly, and only objects may be its keys.** As long as a `Map` is alive, all of its keys and values stay in memory even when nothing else references them, which is a direct route to a leak. In a `WeakMap` an object that becomes unreachable from code is removed by the garbage collector together with its entry. The price is the loss of iteration: there is no `size`, `keys()`, `values()`, `entries()` or `forEach()`, only `set`, `get`, `has` and `delete`. ```javascript const weakMap = new WeakMap(); let user = { name: 'Alice' }; weakMap.set(user, 'User data'); user = null; // the object and its entry are reclaimed by the GC ``` **Key point:** use a `Map` for general purpose dictionaries and caches, use a `WeakMap` to attach data to objects temporarily without leaking memory.Shown above the full answer for quick recall.Answer (EN)Image**The difference between `Map` and `WeakMap` in JavaScript is fundamental: both store "key to value" pairs, but `WeakMap` is built for the special case** where you need to associate data with objects without preventing the garbage collector (GC) from reclaiming those objects. ## Theory ### TL;DR - `Map` keys can be any value, `WeakMap` keys are **objects only**. - A `Map` holds its keys with **strong** references, a `WeakMap` holds them **weakly**. - A `WeakMap` **cannot be iterated**: there is no `size`, `keys()`, `values()`, `entries()` or `forEach()`. - A key object that is unreachable from outside disappears from the `WeakMap` automatically, along with its value. - `WeakMap` methods: `set`, `get`, `has`, `delete`. - Typical tasks: private class data, a leak free cache, internal marks on objects. ### Quick example **First a `Map`:** ```javascript const map = new Map(); let user = { name: 'Alice' }; map.set(user, 'User data'); console.log(map.get(user)); // "User data" user = null; // drop the reference console.log(map); // the Map still holds { name: "Alice" } ``` The object will not be removed from memory while the `Map` keeps a reference to it, and that is precisely a **memory leak**. **Now a `WeakMap`:** ```javascript const weakMap = new WeakMap(); let user = { name: 'Alice' }; weakMap.set(user, 'User data'); console.log(weakMap.has(user)); // true user = null; // the object becomes unreachable // the object will be removed automatically by the garbage collector ``` What happens here: - Once the object is **no longer reachable** from code, the garbage collector removes both the object and its entry in the `WeakMap`. - A `WeakMap` never stands in the way of memory cleanup. ### The main difference | Property | `Map` | `WeakMap` | | --- | --- | --- | | Keys | any values (strings, numbers, objects) | **objects only** | | Reference type | strong (the object lives while a reference exists) | **weak** (the GC may remove the object) | | Iteration (`for...of`, `.keys()`, `.size`) | available | not available | | Size check | `.size` | not available | | Use case | general purpose key value pairs | temporary bindings to objects | ### What a "weak reference" means A weak reference is a reference that **does not prevent the garbage collector from removing an object** when no other, strong references to it remain. In a regular `Map` keys are held strongly: while the `Map` itself exists, all of its keys and values stay in memory. In a `WeakMap` that is not the case, the GC may clear them at any moment. That is exactly why a `WeakMap` refuses to be iterated: its set of keys changes unpredictably, outside the control of your code, so any traversal would be non deterministic. ### WeakMap methods | Method | Description | | --- | --- | | `set(key, value)` | store a value for an object | | `get(key)` | read the value | | `has(key)` | check for presence | | `delete(key)` | remove the entry | **There is no** `size`, `keys()`, `values()`, `entries()` or `forEach()`, because a `WeakMap` is not iterable. ### Practical uses **Storing private data for objects:** ```javascript const privateData = new WeakMap(); class User { constructor(name, age) { privateData.set(this, { age }); this.name = name; } getAge() { return privateData.get(this).age; } } const alice = new User('Alice', 30); console.log(alice.getAge()); // 30 // once alice is gone, the object and its private data leave memory ``` Two benefits apply at once here: - the private data is invisible from the outside, - it is cleaned up automatically when the object is collected. **Caching:** ```javascript const cache = new WeakMap(); function getData(obj) { if (!cache.has(obj)) { const data = Math.random(); // assume this is expensive to compute cache.set(obj, data); } return cache.get(obj); } let user = {}; console.log(getData(user)); // the cache entry is created user = null; // the object and its cache entry are cleared automatically ``` Ideal for holding computed data temporarily with no memory leaks. **In short:** | Property | `Map` | `WeakMap` | | --- | --- | --- | | Key types | any | objects only | | Iterating elements | available | not available | | Binding to objects | strong | weak | | Automatic removal | no | yes | | Use case | caches, dictionaries, configs | private data, leak free caches | ### Common mistakes - **Using a primitive as a `WeakMap` key.** `weakMap.set('id', 1)` throws a `TypeError`, only an object can be a key. - **Looking for `size` or a loop over a `WeakMap`.** They are absent by design; if you need a list of keys, only a `Map` fits. - **Treating a `Map` as a safe object cache.** While the `Map` lives, so do all of its keys, the most common source of leaks in long running applications. - **Expecting instant cleanup.** The GC runs on its own schedule, so right after `user = null` the entry may still exist. - **Confusing it with `WeakSet`.** A `WeakMap` stores "object to value" pairs, a `WeakSet` stores only the objects themselves, with no values. - **Keeping a strong reference to the key inside the value.** If the value reaches its own key through another strong chain, the object never leaves memory.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.