Map
The Map
object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.
Map
Object
Accidental Keys
A Map
does not contain any keys by default. It only contains what is explicitly put into it.
An Object
has a prototype, so it contains default keys that could collide with your own keys if you're not careful.
Key Types
A Map
's keys can be any value (including functions, objects, or any primitive).
Key Order
The keys in Map
are ordered in a simple, straightforward way: A Map
object iterates entries, keys, and values in the order of entry insertion.
Although the keys of an ordinary Object
are ordered now, this was not always the case, and the order is complex. As a result, it's best not to rely on property order.
Size
The number of items in an Object
must be determined manually.
Iteration
Note:
Performance
Performs better in scenarios involving frequent additions and removals of key-value pairs.
Not optimized for frequent additions and removals of key-value pairs.
Examples:
Last updated
Was this helpful?