WeakMap and WeakSet
WeakMap and WeakSet are like Map and Set, but they don’t keep their keys alive. When an object is no longer used elsewhere, it can be cleaned up automatically.
On this page · 5 min · Advanced
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Oct 27, 2026
Related tutorials
Learning objectives#
- Explain what “weak” means
- Store private data per object with WeakMap
- Track objects with WeakSet
- Know their restrictions
Explanation#
A normal Map holds strong references: as long as the Map exists, its keys can never be garbage-collected.
WeakMap and WeakSet hold weak references: if nothing else references the key object, the entry disappears automatically.
Keys must be objects (not strings or numbers), and they are not iterable — you cannot list their contents or get a size.
Typical uses: attaching private or cached data to DOM elements or objects without causing memory leaks.
Code example: Private data and visited tracking#
const privateData = new WeakMap();
class Counter {
constructor() { privateData.set(this, { count: 0 }); }
increment() {
privateData.get(this).count++;
return privateData.get(this).count;
}
}
const c = new Counter();
c.increment();
console.log(c.increment());
console.log(Object.keys(c).length); // nothing visible on the object
const visited = new WeakSet();
const nodeA = { id: 'A' };
visited.add(nodeA);
console.log(visited.has(nodeA), visited.has({ id: 'A' }));
try { new WeakMap().set('key', 1); } catch (e) { console.log(e.name); }
2 0 true false TypeError
Step-by-step explanation#
- Each Counter’s data is stored in the WeakMap, keyed by the instance itself.
- The object has no visible properties, so the data is effectively private.
- WeakSet.has checks identity — a different object with the same content is not found.
- Using a string key throws a TypeError: keys must be objects.
- When c is no longer referenced, its WeakMap entry can be collected automatically.
Real-world use case#
Libraries attach metadata to DOM nodes (for example, “this element has been initialised”) without leaking memory when nodes are removed.
Common mistakes#
Strings and numbers cannot be WeakMap keys.
WeakMap and WeakSet have no forEach, keys() or size, by design.
Best practices#
- Use WeakMap for per-object caches and metadata
- Prefer #private fields for class privacy in new code
- Use WeakSet to mark objects as processed
- Use Map/Set when you need to list or count entries
Remember this#
Weak = “don’t keep this object alive just for me”.
Keys must be objects. No size, no looping.
Perfect for caches and metadata without leaks.
Practice exercise#
Your task: Use a WeakSet to make a function process(obj) that only processes each object once.
Show solution
const done = new WeakSet();
function process(obj) {
if (done.has(obj)) return 'skipped';
done.add(obj);
return 'processed';
}
const o = {};
console.log(process(o), process(o)); // processed skipped
Mini quiz#
Which can be a WeakMap key?
Want more? Practise all JavaScript MCQs · JavaScript interview questions
My notes#
Saved in this browser.