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.
Safe to Use
WeakMap
is considered safe to use.
It’s supported by 94% of global browsers.
Browsers
Servers
Version Breakdown
Full Support
Full Support
Full Support
Full Support
Full Support
Full Support
Full Support
Full Support
This feature is behind the --harmony
runtime_flag
Full Support
Full Support
Full Support
Full Support
Full Support
Full Support
Full Support
See Also
- Polyfill of
WeakMap
incore-js
- WeakMap object in the Keyed collections guide
- Hiding Implementation Details with ECMAScript 6 WeakMaps
Map
Set
WeakSet