Es6 WeakSet tutorials class examples in javascript | Typescript

WeakSet is a collection class similar to the Set class in ES6. Other classes in this category include are Set, Map and WeakMap..

WeakSet functions as a Set class but specifically stores weak object references. If the reference of an object becomes null, the object is automatically garbage collected. Essentially, WeakSet holds weak references of objects, and if an object is no longer referenced or destroyed, the Garbage Collector automatically removes it from the WeakSet, leading to reduced memory consumption.

TypeScript WeakSet

The WeakSet class is not defined in the built-in TypeScript class.

To utilize ES6 collection classes in TypeScript, certain configurations are necessary. Please add "lib": ["es6"] to the TypeScript configuration file (tsconfig.json), or alternatively, include polyfill.js to provide backend support.

Declaration and Creation of a WeakSet in TypeScript

To create an empty WeakSet object, use the new keyword as follows.

new WeakSet();

Similarly, you can create a Set object using an Iterable object, where the iterable object is an array of collections.

new WeakSet(iterable : Object)

Below is an example of creating a 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 provides methods for:

  • Adding/removing elements.
  • Checking for existence.

However, certain operations such as empty set operations and iterable operations are not implemented.

  • WeakSet.add() method

    This method adds a new object to the WeakSet and returns the 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

    The has() method returns true if the object already exists in the set; otherwise, it returns false.

    Syntax:

    has(key : Object) : Boolean
    

    Here’s an example usage

    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 an object from the set and returns true if it exists; otherwise, it returns false.

Syntax:

delete(key : Object) : Boolean

Here’s an example usage.

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

WeakSet Allowed and Disallowed Objects in

Primitive types such as String, Boolean, Number, and Undefined are not allowed in Set. Only objects are permitted.

For example:

var set = new WeakSet();
set.add(new Date()); // Date objects are acceptable
set.add({ objectkey: "objectvalue" }); // acceptable
set.add(false); // not acceptable, primitives are not allowed
set.add("string"); // not acceptable, primitives are not allowed
set.add(123); // not acceptable, primitives are not allowed

Difference between Set and WeakSet

Both are used to store a collection of objects. There is an important difference between set vs weakset.

SetWeakSet
Sets stores strong referencesObjects 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() methodsWeakSet did not implement iterator, so objects are not iterable.