Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptWhat is the difference between Map and WeakMap in JavaScript ?

What is the difference between Map and WeakMap in JavaScript ?

In this article, we will talk about the difference between Map and WeakMap which are introduced by ES6. Javascript object supports only one key object. For supporting multiple key objects, Then Map comes on this path. 

Map: A Map is an unordered list of key-value pairs where the key and the value can be of any type like string, boolean, number, etc. For better understanding, we take an example of Map and its properties.

Example: This example shows the implementation of Map in Javascript.

Javascript




// Creating an empty map
const myMap = new Map();
 
// Creating a set by inserting the key-value pair
console.log(myMap);
 
myMap.set("info", { name: "Sam", age: 36 });
 
// Access the elements of a Map
console.log(myMap);
 
console.log(myMap.get("info"));
 
// Checking the element in a Map using has() method
console.log("check whether info is there or not - "
    + myMap.has("info"));
 
// Returning the number of elements using size property
console.log("The no.of elements in a Map are " + myMap.size);
 
// Removing the element from the map using
// clear() and delete() methods
// removing a particular element
myMap.delete("address");
 
myMap.delete("info"); // true
console.log(myMap);
 
// Iteration through the map
// using forEach method()
const map2 = new Map();
map2.set("name", "Sam");
map2.set("age", "36");
 
// looping through Map
map2.forEach(function (value, key) {
    console.log(key + "- " + value);
});


Output

Map(0) {}
Map(1) { 'info' => { name: 'Sam', age: 36 } }
{ name: 'Sam', age: 36 }
check whether info is there or not - true
The no.of elements in a Map are 1
Map(0) {}
name- Sam
age- 36

WeakMap: In a Weak Map, every key can only be an object and function. It used to store weak object references. For better understanding, we take an example of WeakMap and its properties:

Example: This example shows the implementation of Map in Javascript.

Javascript




// Creating a WeakMap
const myweakMap = new WeakMap();
console.log(myweakMap); // WeakMap {}
 
let obj = {};
 
// Adding object (element) to WeakMap
myweakMap.set(obj, 'hello everyone');
 
console.log(myweakMap);
 
// Access the element of a WeakMap using get() method
console.log("The element of a WeakMap - " + myweakMap.get(obj));
 
// Checking the element in a map using has() method
console.log("check if an element is present in WeakMap - "
    + myweakMap.has(obj));
 
// Delete the element of WeakMap using delete() method
console.log("deleting the element of WeakMap - "
    + myweakMap.delete(obj));
console.log(myweakMap); // WeakMap {}
 
// WeakMaps are not iterable. It will return
// an error. For example,
const weakMap1 = new WeakMap();
console.log(weakMap1); // WeakMap {}
let obj1 = {};
 
// Adding object (element) to WeakMap
weakMap.set(obj1, 'hello');
 
// Looping through WeakMap
for (let i of weakMap1) {
 
    console.log(i);  // TypeError
}


Output:

WeakMap { <items unknown> }
WeakMap { <items unknown> }
The element of a WeakMap - hello everyone
check if an element is present in WeakMap - true
deleting the element of WeakMap - true
WeakMap { <items unknown> }
WeakMap { <items unknown> }

ReferenceError: weakMap is not defined

Difference between Map and WeakMap:

Map

WeakMap

A Map is an unordered list of key-value pairs where the key and the value can be of any type like string, boolean, number, etc. In a Weak Map, every key can only be an object and function. It used to store weak object references.
Maps are iterable. WeakMaps are not iterable.
Maps will keep everything even if you don’t use them. WeakMaps holds the reference to the key, not the key itself.
The garbage collector doesn’t remove a key pointer from “Map” and also doesn’t remove the key from memory. The garbage collector goes ahead and removes the key pointer from “WeakMap” and also removes the key from memory. WeakMap allows the garbage collector to do its task but not the Map.
Maps have some properties : .set, .get, .delete, .size, .has, .forEach, Iterators. WeakMaps have some properties : .set, .get, .delete, .has.
You can create a new map by using a new Map(). You can create a new WeakMap by using a new WeakMap().

RELATED ARTICLES

Most Popular

Recent Comments