30

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

Luboš Turek
  • 6,273
  • 9
  • 40
  • 50
  • Possible duplicate of [ES6 Set, WeakSet, Map and WeakMap](http://stackoverflow.com/questions/32710432/es6-set-weakset-map-and-weakmap) – a better oliver Apr 10 '17 at 12:13

5 Answers5

31

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:

  • Sets can store any value. WeakSets are collections of objects only.
  • WeakSet does not have size property.
  • WeakSet does not have clear, keys, values, entries, forEach methods.
  • WeakSet is not iterable.
Luboš Turek
  • 6,273
  • 9
  • 40
  • 50
  • 1
    Are there any other differences? Like memory usage, performance differences? – evolutionxbox Apr 10 '17 at 09:50
  • 2
    `WeakSet is not iterable` then why should we use it and how to parse it? – Vikas Bansal Oct 09 '17 at 10:01
  • 10
    @VikasBansal `WeakSet` allows you to check in O(1) time if any object has already been added to it. You can use that as a boolean check on the object for, say, a memoized method, and the other benefit is that if the object falls out of scope, you're not wasting memory as you would in a normal `Set` because it can then be garbage collected. – Patrick Roberts May 28 '18 at 16:25
11

Summary:

Weaksets are javascript objects which holds a collection of objects. Due to the nature of a set only one object reference of the same object may occur within the set. A Weakset differs from a normal set in the following ways:

  1. Weaksets can only hold objects within its collection, no primitive values (e.g. int, boolean, string) are allowed.
  2. References to the objects are held weak. This means that whenever there is no other reference towards the object besides the WeakSet, the object can be be garbage collected (i.e. the JS engine will free the memory which object the reference was pointing to).

Example:

let myWeakSet = new WeakSet();
let obj = {};
myWeakSet.add(obj); 
console.log(myWeakSet.has(obj));

// break the last reference to the object we created earlier
obj = 5;

// false because no other references to the object which the weakset points to
// because weakset was the only object holding a reference it released it and got garbage collected
console.log(myWeakSet.has(obj)); 
                     
Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155
  • 1
    using *Set* instead of *WeakSet* in your example, will produce the same result! – Saman Mohamadi Sep 14 '19 at 07:36
  • 1
    `myWeakSet` doesn't contain a reference to `obj` at that point, because `obj` points to a primitive value 5. This doesn't illustrate the weak reference behavior of `WeakSet`. – Spike Sagal Oct 04 '20 at 23:31
  • WeakSet will allow you to search in O(1) where as Set will take O(n) time. hence weakset prefered if searching for objects in an array but in this day and age of immutability who would want to do such stuff... why not embrace a safer option of immutability. – Ankit Tanna May 14 '21 at 08:15
4

Set:- A Set is a collection of values, where each value may occur only once. And main method are add, delete, has, clear and size.

WeakSet:- WeakSet objects allows you to store collection of unique key.“WeakSet” keys cannot be primitive types. Nor they can be created by an array or another set. Values of WeakSet must be object reference.

Avnesh Shakya
  • 3,828
  • 2
  • 23
  • 31
  • your answer was not clear. I tried to improve it, but I still think that there is missing information on what a WeakSet is. – Marco Altieri May 27 '18 at 18:10
1
  • Sets allows to store only once.
  • The elements stored in set does not have a key or index. So it is difficult to retrieve an element using default method like get()
  • A WeakSet only accepts objects as its values.
  • A weakset doesnot prevent garbage collection if there aren’t any other references to an object stored (the reference is weak)
0
example will be more clear if written like this :
   
     let myWeakSet = new WeakSet();
     let x = {name:"ali",age:38};
     myWeakSet.add(x);
     x = 5; 
     console.log(myWeakSet);

then:
     let mySet = new Set();
     let x = {name:"ali",age:38};
     mySet.add(x);
     x = 5; 
     console.log(mySet);
    
in the first example console will show you that weakSet contain no objects because another value was assigned to object reference (x) ......but in second example console will show you that Set contain an object ... and by making mySet iterable you can see the properties of object(x) you have added to mySet :

     console.log(mySet.values().next().value);