![]() |
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 MapThe 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
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
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:
|
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |