ES6 Everyday: WeakSets

If you’ve been following along, you might have expected this one: Map, Set, WeakMap, and now (drumroll) WeakSet.

Like Map versus WeakMap, WeakSet is very similar to Set, except for a few key differences:

  • WeakSet can only store objects (no primitive values)
  • WeakSet holds values “weakly” and does not prevent garbage collection on values
  • WeakSet is not iterable

Just like WeakMap, it’s not likely you’ll be using WeakSet too frequently.

Resources