WeakMap
A key-value collection whose keys must be objects and are held weakly, so an entry disappears when its key object is garbage collected.
In more detail
It is ideal for attaching metadata to DOM elements: when the element is removed and forgotten, its data goes too. To make this possible WeakMap cannot be iterated and has no size. Keys can be objects or non-registered symbols.
Related terms
- WeakSet — A collection of objects held weakly, used to mark objects, such as elements already processed, without keeping them alive.
- WeakRef — An object that holds a weak reference to another object; deref() returns the object, or undefined once it has been collected.
- Map (collection) — A built-in JavaScript collection of key-value pairs where keys can be any value, including objects, kept in insertion order.
- garbage collection — The automatic freeing of memory held by objects that the program can no longer reach.
Taught in
- Memory Management: Garbage Collection, Memory Leaks And WeakMap — Frontend Architecture & Web Platform