WeakMap & WeakSet Playground

Keys held weakly: WeakMap notes stick to the deposit boxes at Bayu’s counter, the Map’s ledger hangs on the wall. Let a box go, count the entries, try a text…

Why it matters

The safe way to attach data to an object or a DOM node without leaking memory: private metadata, a cache per object. A plain Map holds its keys for ever; a WeakMap does not. The price: no .size, and no looping over it.

The key idea

A WeakMap holds its keys weakly

Common mistakes

Try it in this playground