What is the Map object for in Javascript?

Asked

Viewed 273 times

6

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

Unlike Set, that I already have an idea of how it works and saw in other languages, the Map is something I didn’t get to see in any language I worked on.

  • What is the object for Map?
  • What sets him apart from one Object common?
  • When should I use the Map?

1 answer

7


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

According to the mozilla page the differences are:

  • The keys of a Object sane Strings and Symbols, but in the Map can be any value, including functions, objects and any primitive.
  • The keys on the map are wages while keys added to the object are not. So, when iterating over it, an object Map returns keys in insert order.
  • You can get the size of a map easily with the property size, while the number of properties in an object must be determined manually.
  • A map is an iterable and can therefore be directly iterated, while iterating on an object requires getting its keys in some way and iterating on them, although it has a function that helps in this, it is not direct.
  • A Object has a prototype, so there are standard keys on the map that can collide with your keys if you’re not careful. From ES5, this can be ignored using map = Object.create(null), but this is rarely done.
  • A Map may perform better in scenarios involving 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 you have a data collection use a Map, or another structure that makes more sense.

Apparently, but without being sure, Object has complexity constant and Map has logarithmic complexity. Probably what I answered in What are the differences between hashed or tree map implementation?.

I say apparently because there’s nothing in specification, that alias is very bad, says nothing about other guarantees that can, in theory be equal to other structures that also do not require certain guarantees, as ordination. There is nothing canonical, only strong evidence that the complexities are these.

Map Objects are Collections of key/value pairs Where Both the 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 is only intended to describe the required observable Semantics of Map Objects. It is not intended to be a viable implementation model.

Gráfico BigO

See examples (but the text apparently has wrong things).

Related. Also. And as it is in Python.

  • Something that deeply bothers me in Map is that we cannot inform a hash function to be used internally. You need to explicitly calculate the hash if you don’t want to use the standard design. It becomes much less convenient and less expressive.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.