I've been looking at the new ES6 docs on MDN and I can't find a real world use for the Map object where a normal object wouldn't work. Does anyone have any use cases for Maps and explain why an object wouldn't work in that scenario?
MDN lists a number of important differences:
- An
Object
has a prototype, so there are default keys in the map. [editor: the good oldhasOwnProperty
issue]- The keys of an
Object
areStrings
, where they can be any value for aMap
.- You can get the size of a
Map
easily while you have to manually keep track of size for anObject
.- A
Map
iterates its elements in insertion order, whereas iteration order is not specified forObjects
.
So a Map
is finally an insert-ordered key-value store for Javascript, which additionally allows mapping any value to any value, instead of restricting keys to be strings. This can greatly simplify some code where ordering is important, or where objects or other complex data types need to be associated with other data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With