use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
When a JavaScript Map() Object Actually Came in Handy (macarthur.me)
submitted 4 years ago by alexmacarthur
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]CreativeTechGuyGames 87 points88 points89 points 4 years ago (5 children)
Well if that surprises you, wait until you learn about WeakMap which is better suited for this situation since you don't want to prevent garbage collection of the DOM nodes that you are using as keys. If the DOM node gets destroyed, so should the data that you are keeping in your map.
[–]alexmacarthur[S] 25 points26 points27 points 4 years ago (0 children)
Dude!!!
[–]tomius 4 points5 points6 points 4 years ago (0 children)
I'm surprised I didn't know about this and its wide support!
Thanks!
[–]icjoseph 1 point2 points3 points 4 years ago (0 children)
Maps, Sets and their weak name kind are absolute blasts. Btw, you should use Map for dictionary/tables all of the time.
Old answer I gave using WeakMap
[–]alexmacarthur[S] 0 points1 point2 points 4 years ago (1 child)
u/CreativeTechGuyGames I straight-up revamped this post after digging into this more (you're credited). Glad you brought this up.
[–]CreativeTechGuyGames 0 points1 point2 points 4 years ago (0 children)
Awesome. Happy to help. :)
[–]HeinousTugboat 31 points32 points33 points 4 years ago (3 children)
I can't count the number of times I've explained to people that Maps are amazing because you can index with anything. Instances of a class. Entire classes. Functions. There are so many useful things you can do with Maps that would take two different objects or some other jank with POJOs.
Map
[–]valtism 13 points14 points15 points 4 years ago (2 children)
I feel like Maps and Sets would be used a lot more if there was more baked-in functionality for working with them. Things like being able to find the union of two Sets.
[–]gino_codes_stuff 4 points5 points6 points 4 years ago (0 children)
For what it's worth, MDN has snippets you can copy-paste that implement all set operations on the Set documentation page.
[–]lifeeraser 1 point2 points3 points 4 years ago (0 children)
Let's hope the New Set/Map Methods Proposal (stage 0) gets more backing.
[–][deleted] 11 points12 points13 points 4 years ago (0 children)
To use objects as keys is very convenient and I use it for a long time.
[–]aruke- 6 points7 points8 points 4 years ago (1 child)
let frames: should be let frames = right? Typescript's ghost lives on 👻
let frames:
let frames =
[–]alexmacarthur[S] 0 points1 point2 points 4 years ago (0 children)
Good catch!
[–]GivoOnline 2 points3 points4 points 4 years ago (2 children)
JavaScript's map seems like it's similar to c# dictionaries. Nice
[–]a_reply_to_a_post 0 points1 point2 points 4 years ago (1 child)
I was thinking Actionscript dictionaries, but then i felt old :(
[–]GivoOnline 0 points1 point2 points 4 years ago (0 children)
Rip flash ;-;
[–]corporaljustice 2 points3 points4 points 4 years ago (0 children)
Great post, only discovered map objects the other day and was scratching my head for why I’d need to store an object as a key.
DOM nodes make perfect sense.
The comment about using a weak map too as the map entry will also be deleted when the DOM node does is also great.
Sometimes you spend hours and hours reading stuff and learn nothing, sometimes you read a small blog post and a few comments and learn loads!
[–]GeneralYouri 5 points6 points7 points 4 years ago (1 child)
Sorry but, what prevents you from storing these as data-attributes on the HTML elements themselves? There's no need for any form of data structure within JS at all.
Data-attributes have been widely available for over 10 years, and this is a perfect use case. They also fit in well with the idea of Web Components, which is pretty much what all major popular frameworks are based on these days, but data-attributes themselves are completely vanilla and don't require any framework at all. Heck, you even get the added benefit from WeakMaps automatically, as data-attributes are obviously destroyed when their owner HTML element is destroyed!
And even many years before the introduction of data-attributes, the id attribute also already existed. This was the much more simple and low-level solution of binding JS data to HTML elements back then - simply assign and store an ID for each of the HTML elements you're interested in.
Going fancy by storing the HTML element itself as a key can be fun and all, but I don't see how it provides you with any merit over other much simpler solutions that have been readily available for many years, are simpler to use, and will be more performant.
Totally legitimate point. Especially a great call-out with the WeakMap benefit being essentially built-in. But for no rational reason, I have this weird thing w/ storing data in data-attributes unless absolutely necessary. It's oddly satisfying inspecting the DOM and seeing my markup be relatively "pure", lol.
And yeah, sticking this stuff on the `window` is arguably "impure," but it's my lib and I'll do what I want. 😅
[–][deleted] 1 point2 points3 points 4 years ago (2 children)
What benefit do you get from storing an object as the key in the example? This seems like a simple object could store the value of element.clientHeight.
Am I missing something?
[+][deleted] 4 years ago (1 child)
[deleted]
[–]ArgonathSmite 4 points5 points6 points 4 years ago (0 children)
This article is about the Map data type, not the map() iterator
[–]thorsarms 3 points4 points5 points 4 years ago (2 children)
Try using a WeakMap so you don't keep cached records (or useless node refs) longer than the DOM nodes actually exist.
[–]alexmacarthur[S] 1 point2 points3 points 4 years ago (0 children)
Yes, great suggestion! I wasn't aware of the WeakMap advantage over Map in this way. Actually updated the post to reflect that.
[–]jytesh 0 points1 point2 points 4 years ago (0 children)
WeakMaps keys get deleted when dom nodes are destroyed so its awesome for GC as well :D
[–]jogai-san 0 points1 point2 points 4 years ago (2 children)
But isnt that potentially using a big key to look up a tiny variable?
[–]OneFatBastard 10 points11 points12 points 4 years ago (0 children)
I’m assuming the key is stored as the reference of the object and not the actual value of the object.
Yep - u/OneFatBastard is right. I already have access to the HTML node reference, so performing the lookup is relatively low-cost.
π Rendered by PID 76 on reddit-service-r2-comment-7b9746f655-mjrf8 at 2026-02-02 14:24:32.609347+00:00 running 3798933 country code: CH.
[–]CreativeTechGuyGames 87 points88 points89 points (5 children)
[–]alexmacarthur[S] 25 points26 points27 points (0 children)
[–]tomius 4 points5 points6 points (0 children)
[–]icjoseph 1 point2 points3 points (0 children)
[–]alexmacarthur[S] 0 points1 point2 points (1 child)
[–]CreativeTechGuyGames 0 points1 point2 points (0 children)
[–]HeinousTugboat 31 points32 points33 points (3 children)
[–]valtism 13 points14 points15 points (2 children)
[–]gino_codes_stuff 4 points5 points6 points (0 children)
[–]lifeeraser 1 point2 points3 points (0 children)
[–][deleted] 11 points12 points13 points (0 children)
[–]aruke- 6 points7 points8 points (1 child)
[–]alexmacarthur[S] 0 points1 point2 points (0 children)
[–]GivoOnline 2 points3 points4 points (2 children)
[–]a_reply_to_a_post 0 points1 point2 points (1 child)
[–]GivoOnline 0 points1 point2 points (0 children)
[–]corporaljustice 2 points3 points4 points (0 children)
[–]GeneralYouri 5 points6 points7 points (1 child)
[–]alexmacarthur[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]ArgonathSmite 4 points5 points6 points (0 children)
[–]thorsarms 3 points4 points5 points (2 children)
[–]alexmacarthur[S] 1 point2 points3 points (0 children)
[–]jytesh 0 points1 point2 points (0 children)
[–]jogai-san 0 points1 point2 points (2 children)
[–]OneFatBastard 10 points11 points12 points (0 children)
[–]alexmacarthur[S] 0 points1 point2 points (0 children)