Matías Fidemraizer Matías Fidemraizer - 1 month ago 8
Javascript Question

Is Set a hashed collection in JavaScript?

I was asking myself this question. Is

a hashed collection in JavaScript?

For example,
Set.prototype.has
will iterate the entire
Set
or do its implementations use an internal hash table to locate an item within the collection?

Answer

The ECMAScript 2015 specification says that:

Set objects must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection.

Obviously they can't force a particular JS engine to actually do that, but in practice JS engines will do the right thing.

Comments