What is the difference between Map and WeakMap in JavaScript ?
Last Updated :
17 Apr, 2023
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
const myMap = new Map();
console.log(myMap);
myMap.set( "info" , { name: "Sam" , age: 36 });
console.log(myMap);
console.log(myMap.get( "info" ));
console.log( "check whether info is there or not - "
+ myMap.has( "info" ));
console.log( "The no.of elements in a Map are " + myMap.size);
myMap. delete ( "address" );
myMap. delete ( "info" );
console.log(myMap);
const map2 = new Map();
map2.set( "name" , "Sam" );
map2.set( "age" , "36" );
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
const myweakMap = new WeakMap();
console.log(myweakMap);
let obj = {};
myweakMap.set(obj, 'hello everyone' );
console.log(myweakMap);
console.log( "The element of a WeakMap - " + myweakMap.get(obj));
console.log( "check if an element is present in WeakMap - "
+ myweakMap.has(obj));
console.log( "deleting the element of WeakMap - "
+ myweakMap. delete (obj));
console.log(myweakMap);
const weakMap1 = new WeakMap();
console.log(weakMap1);
let obj1 = {};
weakMap.set(obj1, 'hello' );
for (let i of weakMap1) {
console.log(i);
}
|
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(). |
Share your thoughts in the comments
Please Login to comment...