Luboš Turek
Luboš Turek

Reputation: 6615

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?

Upvotes: 33

Views: 16114

Answers (5)

TAREK KOTB
TAREK KOTB

Reputation: 1

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 the Set contained 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);

Upvotes: -1

Willem van der Veen
Willem van der Veen

Reputation: 36640

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)); 
                     

Upvotes: 12

Rahul Gupta
Rahul Gupta

Reputation: 51

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.

Upvotes: 4

ADHITHYA SRINIVASAN
ADHITHYA SRINIVASAN

Reputation: 105

  • 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)

Upvotes: 1

Luboš Turek
Luboš Turek

Reputation: 6615

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.

Upvotes: 35

Related Questions