Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptWhat is the use of a WeakMap object in JavaScript ?

What is the use of a WeakMap object in JavaScript ?

What is a WeakMap?

A WeakMap is a collection of key/value pairs where the keys are objects and values are any JavaScript object.

It is similar to Map with some differences:

  • It is not enumerable and the keys are private in nature.
  • Allows garbage collection of any values if keys are not referenced from somewhere other than a WeakMap.
  • Only objects are allowed as keys.
  • It depends on garbage collection, introducing non-determinism.

Note: Keys cannot be Primitive data type (E.g. a string cannot be a WeakMap key).

Syntax:

new WeakMap([iterable])

Instance methods: It has 4 instance methods.

  1. WeakMap.prototype.delete( key ): It removes any value with the given key. 
  2. WeakMap.prototype.get( key ): It returns the value with the given key, else undefined.
  3. WeakMap.prototype.has( key ): It returns a boolean value whether a value is a WeakMap object or not.
  4. WeakMap.prototype.set( key, value ): It sets the value of the given key in the WeakMap object.

Uses of WeakMap:

  • It can be used in tracking object calls.
  • It can be used in tracking libraries.
  • It can be used as private property in a class.
  • Keeping data about host objects like DOM nodes in the browser.

Note: Execute the below examples with node.js

Steps:

  • Install node.js
  • Execute node <filename.js> in CLI

Example 1: This is a basic use case of a WeakMap where we are referencing an empty object as a WeakMap key which gets garbage collected when assigned to “null” and automatically the WeakMap also gets set to “undefined” from “value”.

Javascript




const wm = new WeakMap();
let obj = {};
wm.set(obj, "value");
console.log(wm.get(obj));
obj = null;
console.log(wm.get(obj));


Output :

value
undefined

Example 2: In this example, you will understand to track objects with weakmap. You can count the number of times the object is passed to the function foo().

Javascript




const objTracker = (() => {
    const wm = new WeakMap();
    return (obj) => {
        wm.set(obj, (wm.get(obj) || 0) + 1);
        return wm.get(obj);
    }
})();
 
function foo(args) {
    const count = objTracker(args);
    console.log(`${JSON.stringify(args)} is called ${count} times`)
}
 
const obj1 = { key: 1 }
const obj2 = { key: 2 }
 
foo(obj1);
foo(obj1);
foo(obj2);


Output:

{"key":1} is called 1 times
{"key":1} is called 2 times
{"key":2} is called 1 times

Example 3: In this example, we create private props in class. This is similar to using private property with # as a prefix.

Javascript




const Foo = (() => {
    const wm = new WeakMap();
    return class Fooclass {
        constructor() {
            wm.set(this, {
                classname: 'Fooclass'
            });
        }
        get name() {
            return wm.get(this).classname;
        }
    }
})();
 
const obj1 = new Foo()
console.log(obj1.name)


Output:

Fooclass

Example 4: In this example, you will understand to do a special task with respect to the library methods, so that when they are called you can do something special around it. You can do something till the mod() function exists. 

Javascript




function mod(a, b) {
    return a % b;
}
 
const lib = new WeakMap();
lib.set(mod, {
    do: () => {
        console.log('Something done');
    }
})
lib.get(mod).do();


Output:

Something done

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments