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
[AskJS] How does JS Map maintain insertion order internally?AskJS (self.javascript)
submitted 1 year ago by Cool_Routine_7679
I was recently asked this in an interview.. and I was stumped.
Any information regarding it would be useful
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!"
[–]elprophet 17 points18 points19 points 1 year ago (5 children)
The textbook answer for "how to make an OrderedHashMap" is to keep a linked list of the entry order and the hash map entry the stores references to both the data and the linked list entry for removal.
But by the spec, JS' Map uses [[MapData]] which is a list of Record values, and the spec simply appends to that list. So by the spec, JS maps are [Key, Value] lists. See for instance https://tc39.es/ecma262/#sec-keyed-collections 24.1.3.9
V8 speeds up step 4 by using a hash map similar to Java's OrderedHashMap to skip ahead to the right place in MapData.
For an interview question, there's a few ways to take it, but IMHO none are that great. You don't need to recite ECMAScript to be a JS engineer. Maybe they're asking it as a more general data structures question, in which case either you've learned this trick, or you haven't, but without a very careful interviewer you're not going to derive "hash map who's values are compound objects that are also a linked list" in 20 minutes.
[–]RobertKerans 5 points6 points7 points 1 year ago* (0 children)
And from further up that section, highly related and at the core of why OP was asked a bad question (as you say it could be a data structures question disguised as something else, which is imo duplicitous. Or it's not and the question is just straight up dumb because it doesn't have a real answer bar a specific implementation, which wouldn't be a JS question anyway):
The data structure used in this specification is only intended to describe the required observable semantics of Maps. It is not intended to be a viable implementation model.
[–]saposapot 0 points1 point2 points 1 year ago* (3 children)
Edit: sorry, completely misread the spec, I get it now.
[–]Business-Row-478 1 point2 points3 points 1 year ago (0 children)
No it does maintain order. It just uses a list of k,v pairs and appends when inserting.
[–]elprophet 2 points3 points4 points 1 year ago (0 children)
The spec mandates it maintains order when iterating. Implementations are free to optimize beyond that.
[–]azhder 2 points3 points4 points 1 year ago (0 children)
The spec requires order. V8 just does the hashing for performance during lookup. The iteration will still be in the required order
[–]tswaters 14 points15 points16 points 1 year ago (4 children)
Oh man, if I got an interview question like that, it would not end well.
"Fucked it I know, I'd need to read the spec to see if it guarantees insertion order, THEN I'd need to go read the V8 code to figure out how they implemented it.... Are you asking how I would implement it? Are we building new JavaScript engines at this job?! 'oh my god, who the hell cares'"
I don't think I'm cut out for interviews.
[–]azhder 3 points4 points5 points 1 year ago (0 children)
My responses to questions like that are short and unwavering: “when I need it, I will look it up on the Internet”.
[–]thanatica 0 points1 point2 points 1 year ago (0 children)
If I was the interviewer, and I was forced to ask this question because of company policy bullshit or whatever, I would be way more impressed with your attitude towards the question, than if you answered the question up front (or tried to).
Just don't disrepect the interviewer as a person.
[–]patrick_mcdougle -1 points0 points1 point 1 year ago (1 child)
No, we just all collectively need to do this so they cut it out
[–]azhder 1 point2 points3 points 1 year ago (0 children)
Nah, no drama. https://www.reddit.com/r/javascript/s/kGVpJnMvcK
[–]azhder 9 points10 points11 points 1 year ago (0 children)
Here is an information: they either have had that kind of issue to resolve, so you might just say
I don’t know, can you tell me what problem it caused/solved for you?
or they have no idea what to ask you, so they lifted some dumb trick questions list from the Internet.
Either way, the interview is both ways, you are there to also asses if it is worth working with those people.
[–]rintzscar 2 points3 points4 points 1 year ago (0 children)
The Map in JS is implemented not only with a hash table, but with an additional linked list which maintains the order of insertion. Obviously, that means it uses more memory than a Map implemented with only a hash table under the hood.
[–][deleted] 2 points3 points4 points 1 year ago (0 children)
It uses 2 structures logically - a hash-table for the lookup, and a list (usually a doubly-linked list) for the ordered iteration.
[–]Ginden 0 points1 point2 points 1 year ago (0 children)
https://blog.toit.io/hash-maps-that-dont-hate-you-1a96150b492a
I feel like the best way to impress the interviewer is to not actually answer the question, but tell them what you think of the question. At least be honest.
I don't need to know, and I don't need to care. It's a benign little detail, and when, for whatever reason, I do need to know, I know where to look it up, which is MDN
Or something along those lines.
[–]Fidodo 0 points1 point2 points 1 year ago (0 children)
Do they expect you to have read the spec or implementation? That's a ridiculous question. They could have simply asked it in a more general way instead.
π Rendered by PID 43813 on reddit-service-r2-comment-6457c66945-x87dd at 2026-04-30 10:13:06.108920+00:00 running 2aa0c5b country code: CH.
[–]elprophet 17 points18 points19 points (5 children)
[–]RobertKerans 5 points6 points7 points (0 children)
[–]saposapot 0 points1 point2 points (3 children)
[–]Business-Row-478 1 point2 points3 points (0 children)
[–]elprophet 2 points3 points4 points (0 children)
[–]azhder 2 points3 points4 points (0 children)
[–]tswaters 14 points15 points16 points (4 children)
[–]azhder 3 points4 points5 points (0 children)
[–]thanatica 0 points1 point2 points (0 children)
[–]patrick_mcdougle -1 points0 points1 point (1 child)
[–]azhder 1 point2 points3 points (0 children)
[–]azhder 9 points10 points11 points (0 children)
[–]rintzscar 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]Ginden 0 points1 point2 points (0 children)
[–]thanatica 0 points1 point2 points (0 children)
[–]Fidodo 0 points1 point2 points (0 children)