Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities
This paper also participates inProject DigginTo win the creative gift package and challenge the creative incentive money
preface
Hello, everyone. Previously, we have shared and introduced the characteristics and usage of Set, WeakSet, Map and WeakMap data structures respectively. In order to make friends more directly understand the similarities and differences of the four data structures, this article will make a unified summary and comparison of the characteristics of the four data structures.
The characteristics of
- Set
- A Set is essentially a constructor that takes an array (or some other data structure with an Iterable interface) as an argument to initialize the Set
- The values in a Set can be of any type, but must not be duplicated. All values are unique and can therefore be used for array de-reuse
- NaN and NaN are considered to be the same value in Set, so there can only be one NaN value in Set
- Two objects in a Set are never equal, even if the key and value are the same
- Set can also be de-duplicated for strings
- No conversion occurs when adding values to a Set, so “5” and “5” are two values
- Set is traversable
- WeakSet
- WeakSet is a constructor that takes an array of object values (or some other data structure with an Iterable interface) as a parameter to initialize A WeakSet
- WeakSet value can only be object type, not other types (basic data type)
- WeakSet has no size attribute and cannot be traversed
- The value in WeakSet can not be repeated. Like Set, two objects in WeakSet are never equal, even if the key and value are the same
- WeakSet objects are weak references, that is, garbage collection mechanism does not consider WeakSet’s reference to the object, that is, if other objects no longer reference the object, then garbage collection mechanism will automatically recover the memory occupied by the object, not considering the object still exists in WeakSet.
- Map
- A Map is a collection of key-value pairs. The key-value pairs are not limited to strings, but can be of any data type
- The Map uses the set method to add members and the GET method to get members
- Map is also a constructor. It can also take an array as an argument, but the members of that array must also be arrays representing key-value pairs. For example: [[” name “, “Alvin”], [18] “age”,]
- Any data structure that has an Iterator interface and each member is a two-element array can be taken as an argument to the Map constructor, not just an array. This includes the Set and Map itself.
- If the same key is assigned more than once, the subsequent value overrides the previous value
- Returns undefined if a nonexistent key is read
- Only references to the same object are treated as the same key by the Map. That is, two objects with the same memory address are considered to be the same key. (Map keys are actually bound to memory addresses, as long as memory addresses are different, they are treated as two keys.)
- If the Map’s key is of a basic type (number, string, Boolean, etc.), then the Map counts as the same key as long as the two values are exactly the same (both type and value are equal). For example, 0 and -0 are the same key, and true and “true” are two keys
- Undefined and NULL are two different keys
- NaN is treated as the same key in the Map
- WeakMap
- WeakMap, like Map, also uses set method to add members and GET method to obtain members
- WeakMap is also a constructor and can also take an array as an argument to the constructor
- WeakMap only accepts objects as key names (except null) and does not accept values of other types as key names (Map is any type)
- WeakMap key names point to objects that are weak references and are not included in the garbage collection mechanism. Therefore, as long as all other references to the referenced object are cleared, the garbage collection mechanism frees the memory occupied by the object. In other words, once it is no longer needed, the key name object and the corresponding key value pair in WeakMap will disappear automatically, without manually deleting the reference
- WeakMap has no size attribute and cannot be traversed
Attribute method
- Set
- add(value)
- delete(value)
- has(value)
- clear()
- size
- keys()
- values()
- entries()
- forEach()
- WeakSet
- add(value)
- delete(value)
- has(value)
- Map
- set(key, value)
- get(key)
- has(key)
- delete(key)
- clear()
- size
- keys()
- values()
- entries()
- forEach()
- WeakMap
- set(key, value)
- get(key)
- has(key)
- delete(key)
conclusion
In this paper, we do a unified arrangement and summary of the characteristics and methods of the four kinds of data we have learned before, so that it is not more intuitive to see the similarities and differences between them, but also more convenient to understand them. Thank you for your support.
Like small partners welcome to praise the message plus attention oh!