It's time to ditch Medium for good! 🌈⚡️

Introducing Devblog by Hashnode. Blog on your domain for FREE. Highly customizable and optimized for developers.

Learn more

Are objects in JavaScript just HashMaps?

RE:

I thought so, but according to StackOverflow, they're actually not hashmaps in the way that you would typically expect:

stackoverflow.com/questions/368280/javascri..

  • The key is converted to a string, which isn't what you'd usually think of for hashing (and according to Wikipedia, it isn't, because the result is not fixed-size).

    var q = {}
    q[1] = 42
    console.log(q['1'])  // 42
    
  • When two objects have the same string representation, there's no further equality checking.

    function P (a) {
      this.a = a
    }
    o1 = new P(1)
    o2 = new P(2)
    
    var q = {}
    q[o1] = 42
    console.log(q[o2])  // 42
    

So they're different objects, but the hashmap doesn't notice.

It does seem to use a hashtable (not sure that's a standard or an implementation detail), so it is fast.

You could use your own hashing function and {} use it as a hashmap that way, if you're sure your hashing function yields unique keys (that aren't keywords). Either wrap the hashing function around all map accesses, or overwrite toString (obviously has the side effect over changing the string representation).

Or only use specific types like strings and numbers as keys.

As an additional note, it seems that ECMAScript 6 added special maps, so it seems there was agreement that objects were not, in fact, maps: 2ality.com/2015/01/es6-maps-sets.html . It seems this uses reference equality instead, which is the other extreme and treats identical clones as distinct. But JS generally doesn't do object equality properly, so what can you really expect...

Show all replies

Thanks, I suspected it may be an implementation detail. According to SO, browsers do use hashtables, and it's hard to imagine them using something slower.

As for the last part, I meant you can use integers for indexing, not that they're stored as integer. But still a useful note, since it means that q[1] is the same as q['1'].

Reply to this…

(2 answers) Take me to the question