Es6 Beginner guide to WeakSet class examples in javascript | Typescript WeakSet

WeakSet in ES6

WeakSet is one of the collection class similar to Set class, Other Classes are Set, Map and WeakMap.WeakSet is a Set class which stores collection weak object references,  that is if the reference of the object is null, the object is automatically garbage collected. So WeakSet saves weak references of an object. if the object is referenced/destroyed, Garbage collector automatically removes the object from  WeakSet and memory consumption is less.

Typescript WeakSet

WeakSet class is not defined in the inbuilt class of typescript.

We can use ES6 collection classes in typescript, For this, We need to add some configurations. Please add “lib”: [“es6”] of typescript configuration file - that is  tsconfig.json file,  or you can add polyfill.js for this backend support. 

WeakSet Declaration and creation 

Empty WeakSet is created using the new keyword

new WeakSet()

Can also create a Set object using Iterable object and the iterable object is an array of collections

`new WeakSet`(iterable : Object)

Here is an example Creating WeakSet from an array of objects

var obj1 = { id: 1 };
var obj2 = { name: 'kiran' };
var weakSet = new WeakSet([[obj1, obj2]]);


WeakSet provided methods for adding/removing, check keys, empty set operations. iterable operations are not implemented.

WeakSet.add() method

 It adds a new object to Set  and returns newly modified set Syntax

add(value : Object) : WeakSet

Example Following is an example of add() method

const weakSet = new WeakSet();
const obj = {id:1};

Output is

[[Entries]]: Array(1)
0:value: {id: 1}

WeakSet.has() method 

has() method returns true if the object already exists in a set, else false Syntax

has(key : Object) : Boolean

Here is an example usage of WeakSet has() method

const weakSet = new WeakSet();
const obj = {id:1};
console.log(weakSet.has(obj));// true

WeakSet.delete() method

This method deletes the object from a set and returns true if exists, else false. Syntax

delete(key : Object) : Boolean

Following is an example for delete() method

const weakSet = new WeakSet();
const obj = { id: 1 };
console.log(weakSet.delete(obj));// true

Allowed and Not allowed objects in Set

Primitive types - String, Boolean, Number, Undefined and Symbole Objects are not allowed for storing it in Set. Only objects are allowed

var set = new WeakSet();
set.add(new Date())  // Date objects are  acceptable
set.add({ objectkey: 'objectvalue' })// acceptable
set.add(false) // not acceptable, primitive not allowed to store in set
set.add('string') // not acceptable, primitive not allowed to store in set
set.add(123) // not acceptable, primitive not allowed to store in set

Difference between Set and WeakSet

Both used to store a collection of objects



Sets stores strong references

Objects oof WeakSet are weak referenced/td>

Memory Consumption is high

Memory Consumption is less, the objects will be automatically garbage collected if any objects are unreferenced

Objects to store in Set will allow Primitive type like Strings, numbers, Date, Symbols, Functions, Objects, and Dates

Objects cannot be Primitive types and Symbol object, Other objects are allowed

Set has iterator implementation methods like keys(),values(), entries() and forEach() methods

WeakSet not not implemented iterator, so objects are not iterable

Similar Posts