Sharing Top Content from the Angular-sphere.

V8 JavaScript Engine: Optimizing hash tables: hiding the hash code

  • This post details the recent improvements in how V8 v6.3+ stores the keys in hash tables.
  • For JavaScript objects that were used as keys, previously, the hash code was stored as a private symbol on the object.
  • V8 could also optimize the hash code symbol lookup just like any other property lookup using the IC system, providing very fast lookups for the hash code.
  • The easiest solution to storing the hash code would be to extend the size of a JavaScript object by one word and store the hash code directly on the object.
  • Instead, we could try to store the hash code in the elements store or properties store.

ECMAScript 2015 introduced several new data structures such as Map, Set, WeakSet, and WeakMap, all of which use hash tables under the hood. This post details the recent improvements in how V8 v6.3+ stores the keys in hash tables.

ECMAScript 2015 introduced several new data structures such as Map, Set, WeakSet, and WeakMap, all of which use hash tables under the hood. This post details the recent improvements in how V8 v6.3+ stores the keys in hash tables.

A hash function is used to map a given key to a location in the hash table. A hash code is the result of running this hash function over a given key.

In V8, the hash code is just a random number, independent of the object value. Therefore, we can’t recompute it, meaning we must store it.

For JavaScript objects that were used as keys, previously, the hash code was stored as a private symbol on the object. A private symbol in V8 is similar to a , except that it’s not enumerable and doesn’t leak to userspace JavaScript.

This worked well because we didn’t have to reserve memory for a hash code field until the object was added to a hash table, at which point a new private symbol was stored on the object.

V8 could also optimize the hash code symbol lookup just like any other property lookup using the IC system, providing very fast lookups for the hash code. This works well for monomorphic IC lookups, when the keys have the same hidden class. However, most real-world code doesn’t follow this pattern, and…

V8 JavaScript Engine: Optimizing hash tables: hiding the hash code

Comments are closed, but trackbacks and pingbacks are open.