What is the JavaScript Map object for?

4

This also seems to be another JavaScript novelty: the object Map .

Unlike the Set , which I already I have an idea how it works and I saw it in other languages, Map is something I did not see in any language I worked on.

  • What is the object Map ?
  • What sets it apart from a% common%?
  • When should I use Object ?
asked by anonymous 12.07.2018 / 19:36

1 answer

3

They are almost identical in general, a Object is a map, but not exactly the Map available structure, so there are implementation details that sets them apart, but they serve the same thing.

According to the Mozilla page the differences are:

  • The keys of a Object are Strings and Symbols , but Map can be any value, including functions, objects, and any primitive.
  • The keys on the map are sorted as long as the keys added to the object are not. So, when iterates over it, a Map object returns keys in insertion order.
  • You can get the size of a map easily with the size property, while the number of properties in an object must be determined manually.
  • A map is an iterable one and can therefore be directly iterated, while iterating over an object requires obtaining its keys somehow and iterating over them, even though it has a function that helps in this, is not straightforward.
  • A Object has a prototype, so there are standard keys on the map that can collide with your keys if you are not careful. From ES5, this can be ignored using map = Object.create(null) , but this is rarely done.
  • A Map can perform better in scenarios that involve frequent addition and removal of key pairs.

It's usually a semantic question. For something that looks more like a simple object use Object , when it has a collection of data use a Map , or another structure that makes more sense.

Apparently, but without being sure, Object has constant complexity and Map has logarithmic complexity . Probably what I replied in What are the differences between map implementation by hashes or trees? .

I say apparently because there is nothing in the specification , which alias is very bad, does not say anything about other guarantees that can in theory be equal to other structures that also do not require certain guarantees, such as ordering. There is nothing canonical, only strong hints that the complexities are these.

  

Map objects are collections of key / value pairs where both keys and values may be arbitrary ECMAScript language values. A distinct key value may only occur in one key / value pair within the Map's collection. Distinct key values are discriminated using the SameValueZero comparison algorithm.

     

Map object 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. The data structures used in this Map objects specification are only intended to describe the required observable semantics of Map objects. It is not intended to be a viable implementation model.

See examples (but the seemingly there are things wrong).

Related . Also . And what it's like in Python .

    
12.07.2018 / 19:48