Open In App

Internal Working of Map in JavaScript

Last Updated : 08 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The map is a collection of elements where each element is stored as a Key, value pair. Map objects can hold both objects and primitive values as either key or value. In this article, we will check the internal working of a Map in JavaScript.

Internal Working of Map

The Map in JavaScript allows for storing key-value pairs, providing efficient operations for retrieval, insertion, and deletion. Any data type can be used as a key, and the insertion order of the elements is preserved.

Maps offer access times that are linearly correlated with the collection’s element count and are implemented in order to meet the standard. Just like sets, maps internally store values as a hash table with a time complexity of O(1) for searching. Additionally, the values can also be stored as a search tree or any other data structure such that the time complexity for searching remains better than O(N).

Strict equality is used to compare the keys, assuring reliable retrieval of linked values. When using the value equality algorithm for retrieval NaN is considered to b equal to NaN and other equality is according to the strict equality operator.

Example: This example shows the basic implementation of the map in JavaScript.

Javascript




let map1 = new Map([
    [1, 2],
    [2, 3],
    [4, 5],
    ]);
for (const item of map1) {
    console.log(item);
}


Output: We can see the elements in the map are stored in the same order as they are defined and the value equality algorithm removes the duplicates before storing the data in the map.

[1, 2]
[2, 3]
[4, 5]

Example 2: This example shows the implementation of map in JavaScript.

Javascript




const mapVal = new Map();
  
for (let i = 0; i < 1000000; i++) {
    mapVal.set([i, i]);
}
  
const arr = Array.from(Array(1000000), (_, i) => i);
let include = arr.includes(999999)
  
console.time('map')
console.log(mapVal.has(999999)); // Output: true
console.timeEnd('map')
  
console.time('Array')
console.log(arr.includes(999999)); // Output: true
console.timeEnd('Array')


Output: In this example, we compared has() method with the includes() method of the array. The map has() method takes less time than the includes() method of the array.

false
map: 0.139892578125 ms
true
Array: 0.52294921875 ms

Importance of Map:

  • Accidental Keys & Security: No default keys are stored, only contain what’s explicitly put into them. Because of that, it’s safe to use.
  • Key Types & Order: It can be any value as a key function, object anything. And the order is a straightforward way in the order of entry insertion.
  • Size: Because of the size property a map can be easily retrieved.
  • Performance: Any operation can be performed on math so easily in a better way.
  • Serialization and parsing: We can create our own serialization and parsing support for Map by using JSON.stringify() and JSON.parse() methods.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads