ES6 Maps are underrated!
Javascript Map
is a rarely used data structure, hence it is not as widely understood as it would first appear, also is not as little as it first appears. We will start off with some fundamentals in this post and gradually head towards explaining the Map
’s function as well as certain situations in which it may be helpful.
The Big ⭕ Notation
Understanding Big O Notation is crucial for analyzing the efficiency and scalability of algorithms and data structures. It allows developers to assess how algorithms will perform as the size of the input data increases. The notation provides a standardized way to express the worst-case scenario of an algorithm’s runtime in relation to the size of the input. As developers, it’s essential to choose algorithms and data structures with favorable time complexities to ensure efficient performance, especially when dealing with large datasets.
When considering all this data, it becomes apparent that it’s best for the methods we use not to be dependent on the size of ’n’ data. While these operations may be fast enough to go unnoticed individually, combining multiple operations simultaneously or nested loops can significantly degrade the user experience.
Imagine you’re a chef. Big O Notation is like a recipe that tells you how much more time you’ll need if you decide to cook for a banquet instead of a family dinner. It’s a way to measure the “heaviness” of a recipe. Just like you’d want a dish that can be scaled up easily for a large crowd, in programming, you want algorithms that can handle growing amounts of data gracefully. So, Big O is like a heads-up:
“Hey, this recipe might take a bit longer if you add more guests,"
Array.prototype.find()
🔍
The find()
method in JavaScript performs a linear search on an array. It returns the first element that satisfies the given condition, and it reads through all the data until it finds that element. Therefore, considering the worst-case scenario, where the item we are searching for is the last element of the array, we say it has a time complexity of O(n)
. This situation is exemplified in Aditya Bhargava’s book Grokking Algorithms as follows:
You’re in a huge library filled with books, and you’re tasked with finding specific information. Without a system, you’d have to look through each book one by one, which is time-consuming and inefficient, especially if the library keeps getting more books.
Now, imagine you have a magical index that instantly points you to the exact location of the information you need, no matter how many books are in the library. This magical index is like a hash map in programming. It doesn’t matter if you have 10 books or 10,000; the magical index (hash map) helps you find your information quickly every time, which is why we say it has a time complexity of O(1)—it’s always fast, regardless of the size of the library (or data). 📚✨
Hash Map
🗺️
A hash map is essentially a storage that can map keys to their corresponding values. When you provide it with a key, it returns the value associated with that key, and you can perform insertion and deletion operations using the same key value in these hash tables. So, it’s a very fast storage system and requires O(1)
time complexity for all these operations.
Nevertheless, there are certain considerations to keep in mind. For instance, if keys are distributed using an inefficient hash function, collisions may occur, complicating search operations. Additionally, maintaining a hash table with a fill rate exceeding approximately 70% can result in performance degradation.
Map
> Array
? 🤔
In recent projects, the find()
method is commonly used within user interactive components. For instance, when we need to retrieve data based on parameters from dynamic routes, manipulate specific data, or fetch information selected from dropdowns or input fields, methods like find
(and occasionally filter
) come into play. These operations follow a linear approach and have a time complexity of O(n)
. Now, let’s explore how a Map
can simplify accomplishing these tasks.
|
|
What is a Map
? 🔦
The Map
in JavaScript provides a data structure for mapping keys to values. Unlike arrays, which are indexed by a range of numbers, maps can use any value as a key. This flexibility makes maps useful in various scenarios, especially when dealing with complex data structures or when quick access to data is required.
|
|
Map
using this method and to ensure that our keys are unique, we need to iterate over our data once. The only point we need to pay attention to is that our keys must be unique. Then we have the following result;
|
|
This way, instead of using find
, we can access the value associated with a key in our Map
data structure using get
with a time complexity of O(1)
, independent of the size of the data. Moreover, using the has
method, we can check for the existence of that data with a time complexity of O(1)
instead of using includes
with a time complexity of O(n)
. Keys are case-sensitive, which provides us with convenience.
JavaScript Maps are super underrated.
— Wes Bos (@wesbos) August 31, 2023
Keys of a map can be anything - including an object!
Here I'm using the new groupBy proposed API to group array items into a Map or an Object → pic.twitter.com/RirGlB5akF
Methods of the Map data-structure 📌
|
|
Final Thoughts 💭
In conclusion, both JavaScript Maps
and Arrays
have their own strengths and use cases.
It’s important to note that creating a Map
in JavaScript can be resource-intensive, so it’s only worthwhile if you anticipate using find()
frequently.
Object
and access values that way. This approach is nearly identical to using a Map
, but it has the added benefit of being serializable into JSON
.Lastly, when working with JavaScript for front-end development, it’s worth considering whether excessive focus on performance is a wise decision or not. In my POV, script execution speed rarely becomes the bottleneck for web page or app load times. Rather than vigorously replacing every find()
with a Map
, consider optimizing your functions & APIs or refining your UI to enhance the overall user experience. Prioritizing milliseconds of speed improvement in JavaScript code may not always yield noticeable benefits.