Internal Working of Map in JavaScript
Last Updated :
08 Jun, 2023
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.
Similar Reads
Javascript Map Programming Examples A Javascript 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. When we iterate over the map object it returns the key, and value pair in the same order as inserted.Javascript Map Programmi
3 min read
Applications of Map in JavaScript Javascript 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. When we iterate over the map object it returns the key, and value pair in the same order as inserted. Syntax: new Map([it]); Pa
5 min read
How to iterate over Map elements in JavaScript ? Map() is very useful in JavaScript it organises elements into key-value pairs. This method iterates over each element in an array and applies a callback function to it, allowing for modifications before returning the updated array." The Map() keyword is used to generate the object. The map() method
3 min read
How to Sort a Map in JavaScript? Sorting a Map in JavaScript involves ordering its key-value pairs based on the keys or values. Since Maps maintain the insertion order, you can't directly sort them like arrays. Instead, you'll need to convert the Map into an array, sort it, and then convert it back into a Map.Below are the approach
3 min read
JavaScript map Interview Questions The map() method in JavaScript creates a new array by applying a callback function to each element of the original array, leaving the source array unchanged. It is commonly used for transforming data and returns an array of the same length, making it ideal for functional programming. Here we are goi
4 min read