Saturday 6 March 2021

How do Maps hash object references in JavaScript?

A standard JavaScript object is presumably implemented like a hashmap in every other language — hash of the key modulus the size. This works great for objects, not so much for Maps, as keys can be mutable objects.

Initially, I assumed it would hash the address of the key. Great! However, the address is not static either. When an array or object grows beyond its capacity, it is reallocated in a new memory location.

Given that my "logical" assumption is wrong, how are Maps implemented? Something must be hashed to provide O(1) lookup.

NB: This is not the same as a hashmap, dictionary, or whatever else you'd like to call it. This is specific to the Map object in JavaScript.



from How do Maps hash object references in JavaScript?

No comments:

Post a Comment