javascriptdictionaryv8es6-map

How is object lookup implemented by Map.get(obj) in the javascript v8 engine?


I'm curious about the internals of v8 and how Map is implemented under the hood. Contrasting Maps, javascript objects cannot have objects as keys. As far as I understand it Maps implement lookup in the classic hash map fashion. Some hashing function maps the input key to some output integer, which is used as an index in an array. Then there's some dynamic resizing when the array overflows and some linkedlist in each bin of the array. Probably the details of v8's maps are a little more intricate and they involve some optimizations for small Maps? Curious to know how much the actual implementation differs from my above sketch. Based on the assumption that Map works like this under the hood, how does it map from a plain object to a key in the array? I am guessing it modulos the pointer address into the array?

Curious to dig deeper.


Solution

  • Short answer: ES6 Maps and Sets: how are object keys indexed efficiently? (with a minor update: four years later, the precise way how the random "hash" is stored on objects has indeed been improved; the principle remains the same).

    Long answer: as Bergi suggested, read the source. Note that the details could change at any time (which is one reason why there isn't a whole lot of documentation about what the current implementation does).