Maps Vs Object in Javascript
Posted By : Manisha Kirodiwal | 30-Jul-2018
When to use Map? And when to use Object?
In spite of all the advantages that Map can have against Object, there are still some cases in which Object will have a better performance. Because Object is the central concept of Javascript.
- Object is the best choice for scenarios when we only need a simple structure to store data and we know that all keys are strings or integers (or Symbol), because creating simple objects and accessing the Object property with a specific key is much faster than create a map object.
- Also, in scenarios where there is a need to apply a separate logic to the individual property / element (s), then Object is definitely the option
- For example:
var obj = { id: 1, name: "Hello World!", print: function(){ return `Object Id: ${this.id}, with Name: ${this.name}`; } } console.log(obj.print());//Object Id: 1, with Name: Hello World!.
(If you try to do it with map then you can’t)
- In addition, JSON has direct support for Object, but not with Map (yet). So, in a some situations where we are working a lot with JSON, consider Object as the preferred option over map.
- Otherwise, Map is just hash and object is more than that.The delete operator with the Object property also has several performance problems. Therefore, in scenarios that require a lot of addition and removal (especially) of a new pair, Map can work much better.
- In addition, Map retains the order of its keys, unlike Object, and Map was built taking into account the iteration, so if the iteration or order of the elements are highly significant, consider Map, it will guarantee a stable iteration performance in all browsers. . . .
- And finally, Map tends to have a better performance in the storage of a large data set, especially when the keys are unknown until the moment of execution,and when all the keys and the values are of the same type.
Conclusion
In conclusion, it really depends on the type of data (input) with which you are going to work and what operations you are going to perform on them to prefer one to the other between Map and Object.
The map have more advantages over Object in the situations when we only want a simple search structure for data storage, with all the basic operations it provides. However, Map can never replace Object, in any sense, because in Javascript, Object is, after all, more than just normal hash table .
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Manisha Kirodiwal
Manisha is a Web Application developer and she is good in working with Angular, TypeScript , JavaScript and Jquery. In free time she loves to dance and cooking.