Home > Article > Web Front-end > What is the difference between map and object in es6
Difference: 1. The key of Map can be any value, while the key of Object must be a String or Symbol. 2. The keys in Map are ordered, while the keys in Object are unordered. 3. The number of key-value pairs of Map can be easily obtained through the size attribute, while the number of key-value pairs of Object can only be calculated manually. 4. Map can be iterated directly, but Object cannot be iterated directly. 5. Map performs better in scenarios where key-value pairs are frequently added or deleted, while Object is less efficient.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
Object
In ECMAScript, Object is a special object. It is a top-level object in itself and also a constructor, through which (such as: new Object()) you can create an object. We can think that all objects in JavaScript are an instance of Object. Objects can be declared using the literal method const obj = {}
Map
Map It is a subclass of Object, which can store any type of data in an orderly manner, using key-value pairs to store, where the keys can store any type. A map instance can be obtained through const m = new Map();.
Map is similar to an object and is also a collection of key-value pairs, but the scope of "key" is not limited to strings. Various types of values (including objects) can be used as keys. In other words, the Object structure provides "string-value" correspondence, and the Map structure provides "value-value" correspondence, which is a more complete implementation of the Hash structure. If you need a "key-value" data structure, Map is more suitable than Object. Map can accept an array as parameter. The members of this array are arrays representing key-value pairs.
Object | ||
---|---|---|
The key of a Map can be any value, including function, object or any basic type. | The key of an Object must be a String or Symbol. | |
The keys in Map are ordered. Therefore, when iterating, a Map object returns key values in insertion order. | The keys of an Object are unordered. Note: Since the ECMAScript 2015 specification, objects do preserve the creation order of string and symbol keys; therefore, iterating over an object with only string keys will produce keys in insertion order. | |
The number of key-value pairs in Map can be easily obtained through the size attribute | The number of key-value pairs in Object can only be calculated manually | |
Map is iterable, so it can be iterated directly. | Iterating over an Object requires obtaining its key in some way before it can be iterated. | |
Performs better in scenarios where key-value pairs are frequently added or deleted. | Not optimized in scenarios where key-value pairs are frequently added and deleted. |
javascript advanced tutorial]
The above is the detailed content of What is the difference between map and object in es6. For more information, please follow other related articles on the PHP Chinese website!