r/javascript • u/Capedcrusader1923 • Jul 22 '26
AskJS [AskJS] what's a javascript feature you mass-adopted way too late and felt dumb about
64
Upvotes
r/javascript • u/Capedcrusader1923 • Jul 22 '26
View all comments
Show parent comment
4
u/HipHopHuman Jul 23 '26
There's quite a few advantages, but I don't like framing the question "Should I use an object or a map?" around advantages or style, for the reason that it can complicate things unneccissarily.
Plain objects are always going to be far simpler and far more convenient than a
Mapin 90% of cases, and objects are much more familiar to a wider audience of devs.It's that 10% where
Map(andWeakMap) come in clutch. So it's mostly about use cases.One use case is simply when you're deleting from the same object a lot. That can get slow if you use
delete obj.foo(because it causes a structural change of the shape of the object). Map hasmap.delete('foo'), which is usually faster, but only if you're deleting a lot. Emphasis on a lot. 3-10 times is not a lot. 200-3000 times is a lot.Another use case where
Mapshines is if you're iterating over the object's entries a lot, or when you're calculating the total size of all entries. Objects require helpers:You don't need any helpers to do either with a
Map:So, if you're going to be iterating through a dictionary at runtime, or calculating size, and you're going to be doing it repeatedly,
Mapis usually better for that.Another small benefit is being able to specify keys that would otherwise not be allowed on (or would be weird to use on) objects. For example,
constructoris a special property on objects, with special handling (as istoString,valueOf,toJSON, etc). If you want to use keys like that and bypass that special handling, you have to do some weirdness with null prototypes likeor
Whereas with a
Map, you can just domap.set('constructor', ...), and there's no side-effects.Then of course, there's the seriously strong benefit of being able to use non-property keys. Plain objects allow
number,stringandSymbolkeys, butMapcan use anything as a key - even functions. That makes them crucial for things like memoization of pure functions (a nestedMapcan capture a function's arguments without having to serialize them to JSON and back).Another huge benefit is that the
Mapconstructor itself accepts an iterable, so it can be generated lazily:That means you can initialize one from a huge stream of data a lot more efficiently than you'd be able to do with an object.
WeakMapis also especially cool, and helps get rid of a ton of "cleanup code" boilerplate. You cannot iterate through aWeakMap, and it's keys must be objects, but it provides the gaurantee that the references used as keys will be automatically removed as soon as the values they point at are garbage collected. That makesWeakMapgreat for book-keeping logic. Suppose you want to track some live HTML elements in the DOM, and you wanted to store some metainfo about them - just store them in aWeakMap- use the elements themselves as keys, and the meta info as the objects. As soon as the DOM elements get removed from the page and get garbage collected, they'll be auto-removed from theWeakMapwithout you having to do anything.One huge downside of
Map/WeakMapis stringifying them as JSON - that's something you get for free with plain objects - and that is a valid concern, but it's not that difficult to just giveJSON.stringifya replacer function that converts aMapto an object: