Open In App

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:

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:

Note: Execute the below examples with node.js

Steps:

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”.




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().




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.




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. 




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

Article Tags :