WeakMap
A WeakMap is a collection of key/value pairs whose keys must be objects or non-registered symbols, with values of any arbitrary JavaScript type, and which does not create strong references to its keys. That is, an object's presence as a key in a WeakMap does not prevent the object from being garbage collected. Once an object used as a key has been collected, its corresponding values in any WeakMap become candidates for garbage collection as well — as long as they aren't strongly referred to elsewhere. The only primitive type that can be used as a WeakMap key is symbol — more specifically, non-registered symbols — because non-registered symbols are guaranteed to be unique and cannot be re-created.
WeakMap allows associating data to objects in a way that doesn't prevent the key objects from being collected, even if the values reference the keys. However, a WeakMap doesn't allow observing the liveness of its keys, which is why it doesn't allow enumeration; if a WeakMap exposed any method to obtain a list of its keys, the list would depend on the state of garbage collection, introducing non-determinism. If you want to have a list of keys, you should use a Map rather than a WeakMap.
You can learn more about WeakMap in the WeakMap object section of the Keyed collections guide.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 36 | 12 | 6 | 8 | 36 | 8 | |
| The WeakMap() constructor creates WeakMap objects. | 36 | 12 | 6 | 8 | 36 | 8 |
WeakMap.WeakMap.iterable allowed `new WeakMap(iterable)` | 38 | 12 | 36 | 9 | 38 | 9 |
WeakMap.WeakMap.null allowed `new WeakMap(null)` | 36 | 12 | 37 | 8 | 36 | 8 |
| The delete() method of WeakMap instances removes the entry specified by the key from this WeakMap. | 36 | 12 | 6 | 8 | 36 | 8 |
| The get() method of WeakMap instances returns the value corresponding to the key in this WeakMap, or undefined if there is none. Object values are returned as the same reference that was originally stored, not as a copy, so mutations to the returned object will be reflected anywhere that reference is held, including inside the WeakMap. | 36 | 12 | 6 | 8 | 36 | 8 |
| The has() method of WeakMap instances returns a boolean indicating whether an entry with the specified key exists in this WeakMap or not. | 36 | 12 | 6 | 8 | 36 | 8 |
| The set() method of WeakMap instances adds a new entry with a specified key and value to this WeakMap, or updates an existing entry if the key already exists. | 36 | 12 | 6 | 8 | 36 | 8 |
WeakMap.symbol as keys Non-registered symbols as keys | 109 | 109 | 146 | 16.4 | 109 | 16.4 |
- Before Firefox 38, this method threw a `TypeError` when the key parameter was not an object. This has been fixed in version 38 and later to return `false` as per the ES2015 standard.
- Before Firefox 38, this method threw a `TypeError` when the key parameter was not an object. However, the ES2015 specification specifies to return `undefined` instead. Furthermore, `WeakMap.prototype.get` accepted an optional second argument as a fallback value, which is not part of the standard. Both non-standard behaviors are removed in version 38 and higher.
- Before Firefox 38, this method threw a `TypeError` when the key parameter was not an object. This has been fixed in version 38 and later to return `false` as per the ES2015 standard.
- Before Firefox 38, this method threw a `TypeError` when the key parameter was not an object. This has been fixed in version 38 and later to return `false` as per the ES2015 standard.
Syntax
const privateData = new WeakMap();
class User {
constructor(name) {
privateData.set(this, { name });
}
getName() {
return privateData.get(this).name;
}
} Live demo
Attach metadata to DOM elements
relatedde-ta.. with DOM objectbody change to, WeakMap.
Check whether an object is processed
efficiency to manage.. with object that processed or or WeakMap.
Use cases
-
Using WeakMap
A WeakMap is a collection of key/value pairs whose keys must be objects or non-registered symbols, with values of any arbitrary JavaScript type, and which does not create strong references to its keys.
Cautions
- No specific concerns. Stable across all major browsers.
Accessibility
- When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.