cover-img

JavaScript Hashmap: Performant Key-Value Variable

Key-value pair variable in JavaScript

15 November, 2022

12

12

0

Why hashmap? Well, we all know that another programming language has something called Hashtable. It is a data structure that stores key-value pairs that are so great because of their O(1) constant time lookup. It is an advantageous data structure we can use to store data. In JavaScript, we don't have a built-in data structure like Hashtable. But we can use an object to store key-value pairs. It is called a Map in JavaScript.

What is a Map?

A Map is a collection of elements where each element is stored as a Key, Value pair. A Map's keys and values can be any value (both objects and primitive values). A Map object iterates its elements in insertion order — a for...of loop returns an array of [key, value] for each iteration.
A Map holds key-value pairs where the keys can be any datatype. A Map has a property that represents the size of the map. A Map remembers the original insertion order of the keys.

Map Methods & Property

MethodDescription
set()Sets the value for a key in a Map
get()Returns the value for a key
has()Returns a boolean indicating if a key exists in a Map
delete()Removes a key from a Map
clear()Removes all keys from a Map
values()Returns an iterator object of the values in a Map
keys()Returns an iterator object of the keys in a Map
entries()Returns an iterator object of the key-value pairs in a Map
forEach()Executes a provided function once for each key-value pair in a Map
PropertyDescription
sizeReturns the number of key/value pairs in a Map

Map Example

Map Iteration

Map vs. Object

MapObject
Map is a collection of keyed data items, just like an Object. But the main difference is that Map allows keys of any type.Object keys must be either a string or a symbol.
Map is iterable and can be easily looped over.Object is not iterable and can not be easily looped over.
Map has a built-in forEach method, so there is no need to convert it to an array.Object does not have a built-in forEach method.
Map has a size property, so we can easily get the number of items.Object does not have a size property.
Map can use objects as keys.Object can not use objects as keys.
Map is faster than Object in general.Object is generally slower than Map.

Conclusion

In this article, we have learned about Map in JavaScript. We have learned about the Map methods and properties. We have also known about the difference between Map and Object. I hope you have enjoyed this article. If you have any questions, please leave a comment below. Thank you for reading.

References

javascript

frontend

develevate

bestpractices

toolstipstricks

12

12

0

javascript

frontend

develevate

bestpractices

toolstipstricks

Imamuzzaki Abu Salam
FE @ ITMI | Young Innovator | DevSecOps, ML, & COSS Enthusiast

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.