{

Es6 WeakSet tutorials class examples in javascript | Typescript


WeakSet is one of the collection class similar to Set class in ES6. , Other Classes are Set, Map and WeakMap.

WeakSet is a Set class that stores the collection of 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, the Garbage collector automatically removes the object from WeakSet, and memory consumption is less.

Typescript WeakSet

The WeakSet class does not define in the in-built 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 - tsconfig.json file, or you can add polyfill.js for this backend support.

Declare and create a WeakSet

Empty the WeakSet object created using the new keyword.

new WeakSet()

Similarly, Can also create a Set object using an 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]]);
console.log(weakSet);

WeakSet Methods

WeakSet provided methods for the below things

  • adding/removing
  • checking keys
  • empty set operations and iterable operations are not implemented.

WeakSet.add() method

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

add(value : Object) : WeakSet

Example Following is an example of add() method

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

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);
console.log(weakSet.has(obj));// true
console.log(weakSet.has(null));//false

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 of the delete() method

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

Allowed and Not allowed objects in Set

Primitive types - String, Boolean, Number, Undefined. Symbol Objects are not allowed for storing 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 are used to store a collection of objects. There is an important difference between set vs weakset.

Set WeakSet
Sets stores strong references Objects of WeakSet are weak referenced
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 types like Strings, numbers, Date, Symbols, Functions, Objects, and Dates. Objects cannot be Primitive types and Symbol objects, Other objects are allowed.
Set has iterator implementation methods like keys(),values(), entries() and forEach() methods WeakSet did not implement iterator, so objects are not iterable.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.