Skip to content
Related Articles

Related Articles

What is the difference between Map and WeakMap in JavaScript ?

View Discussion
Improve Article
Save Article
  • Last Updated : 24 Mar, 2022
View Discussion
Improve Article
Save Article

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:

Javascript




<script>
  
    // 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);
    });
</script>

Output:

The output of the above code (Map)

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:

Javascript




<script>
  
    // 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
    }
</script>

Output:

The output of the above code (WeakMap)

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!