Horje
JavaScript WeakMap

A WeakMap in JavaScript is a collection where keys can only be objects or non-registered symbols. It allows values of any type and doesn’t prevent the keys from being garbage collected, making its values eligible for garbage collection when their keys are collected. 

Syntax

new WeakMap()
new WeakMap(iter)

Parameter: It has only one optional parameter.

  • iter: It is an iterable JavaScript object that implements the @@iterator method. It contains two elements where the first is key and the second is value.

Example 1: In this example The myGeeks function creates a WeakMap looseMap, sets objects as keys with names, assigns values, and checks if it has a specific key. Outputs the map and checks for presence of Ram.

JavaScript
function myGeeks() {
    let looseMap = new WeakMap();
    let Ram = {name};
    let Raj = {name};
    let Rahul = {name};
    looseMap.set(Ram, "Ram");
    looseMap.set(Raj, "Raj");
    looseMap.set(Rahul, "Rahul");
    console.log(looseMap);
    console.log(looseMap.has(Ram))
}
myGeeks();

Output:

WeakMap {{…} => 'Raj', {…} => 'Rahul', {…} => 'Ram'}
true

Example 2: In this example, we creates a WeakMap looseMap, sets an object Ram as a key with a value, nullifies Ram, and logs looseMap at different intervals.

JavaScript
let looseMap = new WeakMap();
let Ram = { name };
looseMap.set(Ram, "Ram");
console.log(looseMap);
Ram = null;
console.log(looseMap)
setTimeout(function () {
    console.log(looseMap);
}, 300)

Output: As the reference is removed from the memory so the value in looseMap are garbage collected

WeakMap {{…} => 'Ram'}
WeakMap {{…} => 'Ram'}
WeakMap {}

Supported Browsers:

We have a complete list of Javascript WeakMap methods, to check those please go through this JavaScript WeakMap Complete Reference article.

JavaScript WeakMap – FAQs

What is a WeakMap in JavaScript?

A WeakMap is a collection of key/value pairs where the keys are objects and the values can be arbitrary values. The primary feature of a WeakMap is that it holds “weak” references to the keys, meaning that if there are no other references to the key object, it can be garbage collected.

How do you create a WeakMap?

You can create a WeakMap using the WeakMap constructor.

What are the main differences between Map and WeakMap?

  • Key Type: Map keys can be of any type, while WeakMap keys must be objects.
  • Garbage Collection: WeakMap holds weak references to keys, allowing them to be garbage collected, whereas Map holds strong references.
  • Iterability: Map is iterable, meaning you can loop through its entries. WeakMap is not iterable, and you cannot get a list of its keys or values.

How do you set and get values in a WeakMap?

To set a value in a WeakMap, you use the set method, and to get a value, you use the get method.

Can you use primitive values as keys in a WeakMap?

No, WeakMap keys must be objects. Using a primitive value (like a string, number, or boolean) will result in a TypeError.

How do you check if a WeakMap contains a specific key?

You can use the has method to check if a WeakMap contains a specific key.



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How numbers are stored in JavaScript ? How numbers are stored in JavaScript ?
JavaScript Set() Constructor JavaScript Set() Constructor
When should we use curly braces for ES6 import ? When should we use curly braces for ES6 import ?
WeakSet vs WeakMap in JavaScript WeakSet vs WeakMap in JavaScript
How to display images from an array in JavaScript ? How to display images from an array in JavaScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12