30

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

1

5 Answers 5

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.
3
  • 1
    Are there any other differences? Like memory usage, performance differences? Apr 10, 2017 at 9:50
  • 2
    WeakSet is not iterable then why should we use it and how to parse it? Oct 9, 2017 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. May 28, 2018 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)); 
                     

3
  • 1
    using Set instead of WeakSet in your example, will produce the same result! Sep 14, 2019 at 7: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. Oct 4, 2020 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. May 14, 2021 at 8: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.

1
  • your answer was not clear. I tried to improve it, but I still think that there is missing information on what a WeakSet is. May 27, 2018 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);
1
  • I want to thank @Willem van der Veen as I used his example to explain my idea
    – TAREK KOTB
    Apr 19 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.