javascriptecmascript-6weak-references

What's the difference between ES6 Set and WeakSet?


ECMAScript 6 has these very similar collections: Set and WeakSet. What is the difference between them?


Solution

  • The main difference is that references to objects in Set are strong while references to objects in WeakSet are weak. This means that an object in WeakSet can be garbage collected if there is no other reference to it.

    Other differences (or rather side-effects) are: