The Map object holds key-value pairs, where the key can be of any data type.
The Map object remembers the original insertion order of the keys.
The Map object has properties that represent the size of the map.
Use the new keyword and the Map constructor to create an empty map:
If you want to initialize the instance at the same time as it is created, you can pass an iterable object to the Map constructor, including the key/value logarithm
Group. Each key/value pair in the iterable object is inserted into the new mapping instance in iterative order:
The m1 printed by the console is as follows:
Initialize the map with a custom iterator
The m2 printed by the console is as follows:
After initialization, you can use the set() method to add key/value pairs. Alternatively, you can query using get() and has().
to get the number of key/value pairs in the map through the size property, you can also use delete() and clear() to delete values.
The set() method returns a map instance, so multiple operations can be concatenated, including initialization declarations:
One major difference from the Object type is that Map instances maintain the insertion order of key-value pairs, so they can be enacted according to the insertion order
Row iteration operations. A Map map instance can provide an Iterator that generates arrays of the form [key, value] in insert order.
The Map map instance provides an iterator that generates an array of the form [key, value] in the insertion order. OK
This iterator is obtained by the entries() method (or the Symbol.iterator property, which references entries()):
Because entries() is the default iterator, you can use the scaling operation directly on the map instance to convert the map to an array:
If you do not use an iterator but instead use the callback mode, you can call the mapped forEach(callback, opt_thisArg)
method and pass in callbacks, iterating through each key/value pair in turn.
Most of the features of a Map can be implemented with the Object type, but there are some minor differences. For most business developers, choosing object or map is just a matter of personal preference, but it has little impact. But for developers looking for business and performance, there really is a big difference between object and map. Which one to use in specific practice is still worth carefully screening.
1. Memory occupancy
Given a fixed amount of memory in the same browser, Map can store approximately 50% more key/value pairs than Object.
2. Insertion performance
Inserting new key/value pairs into Object and Map consumes about the same amount, although inserting a Map is generally slightly faster in all browsers
A little. For both types, the insertion speed does not increase linearly with the number of key/value pairs. If the code involves a lot of inserts
, then obviously Map performs better.
3. Find speed
Unlike insertion, the performance difference for finding key/value pairs from large Objects and Maps is minimal, but if only a small number of key/value pairs are included,
Objects are sometimes faster. If your code involves a large number of lookup operations, you may choose in some cases
It’s better to choose Object.
4. Remove Performance
For most browser engines, Map’s delete() operation is faster than insertion and lookup. If your code involves a large number of deletions, you should undoubtedly choose Map.