Map
objects are collections of key-value pairs. A key in the Map
may only occur once; it is unique in the Map
's collection. A Map
object is iterated by key-value pairs — a for...of
loop returns a 2-member array of [key, value]
for each iteration. Iteration happens in insertion order, which corresponds to the order in which each key-value pair was first inserted into the map by the set()
method (that is, there wasn't a key with the same value already in the map when set()
was called).
The specification requires maps to be implemented "that, on average, provide access times that are sublinear on the number of elements in the collection". Therefore, it could be represented internally as a hash table (with O(1) lookup), a search tree (with O(log(N)) lookup), or any other data structure, as long as the complexity is better than O(N).
Key equality
Value equality is based on the SameValueZero algorithm. (It used to use SameValue, which treated 0
and -0
as different. Check browser compatibility.) This means NaN
is considered the same as NaN
(even though NaN !== NaN
) and all other values are considered equal according to the semantics of the ===
operator.