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 mapconst myMap = new Map();// Creating a set by inserting the key-value pairconsole.log(myMap);myMap.set("info", { name: "Sam", age: 36 });// Access the elements of a Mapconsole.log(myMap);console.log(myMap.get("info"));// Checking the element in a Map using has() methodconsole.log("check whether info is there or not - " + myMap.has("info"));// Returning the number of elements using size propertyconsole.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 elementmyMap.delete("address");myMap.delete("info"); // trueconsole.log(myMap);// Iteration through the map// using forEach method()const map2 = new Map();map2.set("name", "Sam");map2.set("age", "36");// looping through Mapmap2.forEach(function (value, key) { console.log(key + "- " + value);}); |
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 WeakMapconst myweakMap = new WeakMap();console.log(myweakMap); // WeakMap {} let obj = {};// Adding object (element) to WeakMapmyweakMap.set(obj, 'hello everyone');console.log(myweakMap);// Access the element of a WeakMap using get() methodconsole.log("The element of a WeakMap - " + myweakMap.get(obj));// Checking the element in a map using has() methodconsole.log("check if an element is present in WeakMap - " + myweakMap.has(obj));// Delete the element of WeakMap using delete() methodconsole.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 WeakMapweakMap.set(obj1, 'hello');// Looping through WeakMapfor (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(). |
