How to Understand WeakMap and WeakSet in JavaScript
A step-by-step guide on how WeakMap and WeakSet work, how they differ from Map and Set, and when to use them for memory-efficient programming.
Understand the Memory Leak Problem with Regular Maps
In a regular Map, if you use an object as a key and that object is deleted or goes out of scope everywhere else in your code, the Map still holds a strong reference to it. This prevents the garbage collector from freeing that object's memory because the Map keeps it alive. For long-running applications that associate metadata with many objects, this can cause gradual memory leaks.
Understand How WeakMap Solves This
A WeakMap holds weak references to its keys. This means the WeakMap does not prevent the garbage collector from reclaiming an object used as a key. If the only remaining references to an object are inside a WeakMap, the garbage collector can collect that object and automatically remove the corresponding entry from the WeakMap. This makes WeakMap ideal for associating data with objects without preventing their cleanup.
Understand WeakMap Restrictions
WeakMap keys must be objects or non-registered Symbols. Primitive values like strings and numbers are not allowed as keys. WeakMap is not iterable, meaning you cannot loop over its entries or get its size. There is no size property, no forEach method, no keys method, and no values method. These restrictions exist because the garbage collector can remove entries at any time, making iteration unreliable.
Use WeakMap for Private Object Data
A practical use of WeakMap is storing private data associated with class instances. Create a WeakMap in module scope, outside the class. In the constructor, use the instance as the key and store private data as the value. Methods access the private data by looking up this in the WeakMap. The private data is completely inaccessible from outside the module. When the instance is garbage collected, the WeakMap entry is automatically removed.
Use WeakMap for Caching Computed Results
WeakMap is excellent for caching expensive computation results keyed by the input object. If you compute some derived property from a DOM element or any object, store it in a WeakMap with the object as the key. On subsequent calls, check the WeakMap first before recomputing. When the object is garbage collected, the cache entry disappears automatically, preventing the cache from growing forever and causing memory issues.
Understand WeakSet
WeakSet is like Set but holds weak references to its values. All values must be objects or non-registered Symbols. Like WeakMap, it is not iterable and has no size property or forEach method. Its main purpose is tracking which objects have been processed or visited without preventing their garbage collection. The only operations available are add, delete, and has.
Use WeakSet to Track Object State Without Leaking
A common use for WeakSet is tracking which objects have already been processed in an algorithm. For example, to prevent processing the same DOM node twice during a tree traversal, add each visited node to a WeakSet and check it before processing. When nodes are removed from the DOM and garbage collected, the WeakSet entries disappear automatically without any manual cleanup.
Know When to Choose WeakMap and WeakSet Over Map and Set
Use WeakMap when you need to associate metadata or cached data with objects and you want that data to be automatically cleaned up when the objects are no longer needed. Use WeakSet when you need to track membership of a group of objects without preventing their cleanup. Use regular Map and Set when you need iteration, size reporting, or when keys and values should persist regardless of external references.
Ready to master this completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

