When to use which data structures in JavaScript
In this article, we will learn about When to use which data structures in JavaScript.
Data Structures in JavaScript
Mainly JavaScript has two data structure Array and Object.
After that two new Data structure introduces in JavaScript which was : Sets and Maps
So, now we have total of four data structures available in JavaScript.
Let's see some cases to indetify which data strctures to use when.
Array:
Case-1:
Use when you need ordered list of values and might contain duplicates
Case-2:
Use when you need to manipulate data because there are a lot of useful array methods
Sets:
Set's looks the same as an array but it also has some scenarios when we consider using sets over the array.
Sets are really not meant to replace arrays but rather to complement them whenever we are dealing with unique values.
Let's see possible cases where we can use sets:
Case-1:
Use when you need to work with unique values
Case-2:
Use hen high-performance is really important because operations like searching for an item or deleting an item from a set can be up to 10 times faster in sets than in arrays.
Case-3:
Use to remove duplicates from an array
Objects
Objects is the traditan key value data strcture before maps introdcues in ES6 but using objects simply as key value stores has a couple of technical disadvantages
Case-1:
Use when you need to include funcitnas or methods
Case-2:
Use when working with JSON(can convert to map)
Maps
Maps on other hand are way better suited for simple key vakue stores because they offer better perforamce in fact
Also map keys can have any data typpe and they are also easy to iterate and its easy to compute the size of map
Case-1:
Use when you simply need to map key to values
Case-2:
Use when you need keys that are not strings
Conclusion:
Do let me know If you face any difficulties please feel free to comment below we love to help you.
If you have any feedback suggestion then please inform us by commenting.
Don’t forget to share this tutorial with your friends on Facebook and Twitter