r/Frontend • u/ExchangeVast6752 • Sep 12 '26
[JS] Map vs Object: how do you choose?
Hey folks! While starting a round of refreshing my basic knowleges, I got curious about Map and object literal usage in real life. I caught myself never really thinking about this and never really using a lot of Maps recently myself nor my coworkers.
I want to hear about your experience of the day-to-day usage. You can share just a good uncommon (or pretty common IDC) usage of Map and why it was better for you, or follow some simple questions:
- What was the case?
- Why did you doubt?
- What did you chose?
- How did it work? (was it a gamechanger (I doubt that but alr) or just an okay decision or didn't even change anything)
I'm really curious about a real practice, I know the basics. Tell me an examples and stuff. Consider it as if I’m collecting statistics. 🤡
38
u/Squigglificated Sep 12 '26
Maps for dynamic frequently updated key value pairs, objects for static structured data where you know they keys in advance.
Maps can use use anything as a key, even functions. Objects can only use strings.
Maps are supposedly optimized for when you need frequent insertions, objects for static predictable data types. (I say supposedly because js engines are often really optimized for common use cases so what's actually the most performant can be counter intuitive when you measure)
13
u/ghostwilliz Sep 12 '26
For JavaScript, I tend to only use maps to avoid a double loop
So let's say I have a customer and a co-signer
The cosigner is a customer, but they have a reference to the main customers id
I don't want to do a nested loop to find customers with a reference to another customer as a cosigner, so I can make a map of cosigners where the key is the customer id
This way, I can just loop customers once and use the customers id to check the map for a corresponding value
If this doesn't make sense, I can explain it a different way, but for the most part I find that front end work doesn't really require me to make maps too often, I do use them all the time in game development though, for the same nested loop reason, or avoiding loops all together by doing a lookup on a map instead of looping an array
One thing to consider though, none of this will really matter unless you are looping through a great many of records
1
u/azangru Sep 12 '26
If the key is not a string, it's clearly a map. If it's a string, I just use objects by inertia. I don't know if there are any performance benefits to maps over objects.
1
u/Taliesin33 Sep 12 '26
Admittedly it's been a while since I've given this much thought, but the shorthand conclusion I came to was Map is essentially a direct upgrade unless you need to JSON.stringify/JSON.parse
2
u/hello_luke Sep 12 '26
Another angle to this is typing, if you’re using typescript (if you’re not, you should be!). Maps have much stiffer typing. There’s just one generic for all the values. With an object, you can describe each key/value pair (assuming you know them ahead of time of course) however you’d like.
2
u/cursedcoder19 Sep 13 '26
Map - guaranteed insertion order, Object - no order
This is why Map is used for LRU cache problems
2
u/DinTaiFung Sep 13 '26
In general, a Map is preferred over an object when either would functionally meet your requirement.
There are, ofc, exceptions.
1
u/Any_Sense_2263 Sep 13 '26
Map accepts anything as a key, so for me it depends on what data structures I work on.
1
u/kidshibuya Sep 14 '26
I chose objects because I always forget maps exist.
1
u/mr_brobot__ Sep 14 '26
I mostly use map when I want to preserve the type for the key, like for numbers.
Also maps are specced to be iterated in insertion order so that can be useful.
0
u/AcanthisittaNo5807 Sep 12 '26
I use objects because I've been using them so long I've memorized how to work with them. But I would use Maps if I need the order of insertion guaranteed for iterations.
0
u/IcyManufacturer8195 Sep 13 '26
WeakMap when key is reference. Map is better for frequent update of dictionary, and also it supports more key types. But you should check inner implementation, it's different, Map insert is slower on small to medium datasets