- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript WeakMap and WeakSet
JavaScript Map & Set
JavaScript WeakMap and WeakSet
WeakMap and WeakSet hold objects without preventing them being cleaned up.
A normal Map keeps its keys alive forever, which can leak memory.
A WeakMap lets the browser reclaim an object once nothing else refers to it.
Example
Example
javascript
const cache = new WeakMap();
const user = { name: "Ada" };
cache.set(user, "some cached data");
console.log(cache.get(user));
console.log(cache.has(user));