About Map and WeakMap

Map

1: What is a Map
        Map is similar to an object, but the key name is not limited to a string. It can be said that the Object structure provides key-value correspondence, and the Map provides value-value correspondence. Therefore, using the Map structure is better than traditional objects.

const dataMap = new Map()
const element = document.querySelector('.node')
dataMap.set(element,'objectData')
console.log(dataMap.get(element))
console.log(dataMap)

        In the above code, we directly pass in the element object when getting the value, and successfully use the object as the key name, which makes up for the shortcomings of traditional objects.

2: Features of Map

  1. Map does not contain any keys by default, all keys are added by themselves. Unlike Object, there is a default key on the prototype chain.
  2. The key of Map can be any type of data, even functions.
  3. The number of key-value pairs in Map can be easily obtained through the size attribute, while Object needs to be calculated manually.
  4. Map performs better than Object in scenarios where key-value pairs are frequently added or deleted.

3: When to use Map

  1. If the key value name you want to add conflicts with the default key value name on Object, and you don’t want to change the name, use Map.
  2. Use Map when data types other than String and Symbol are required as key values.
  3. There are many key-value pairs, and sometimes it is necessary to calculate the number, using Map.
  4. When you need to add and delete key-value pairs frequently, use Map.

WeakMap

1: What is WeakMap

        WeakMap is a new collection type in ES6 called 'weak mapping'. It is a sibling relationship with Map. The difference with Map is this weak word, and the API is still the API of Map.

What is a weak reference

A reference that cannot be guaranteed that the object it refers to will not be reclaimed by the garbage collector. An object that is only referenced by weak references is considered inaccessible and therefore may be collected at any time.

That is to say, when we create a weakly referenced object, we can quietly wait for it to be reclaimed by the garbage collector.

In general, the situation WeakMap maintains a weak reference to the object referenced by the key name, that is, the garbage collection mechanism does not take this reference into account. As soon as other references to the referenced object are cleared, the garbage collection mechanism frees the memory occupied by the object. That is to say, once it is no longer needed, the key name object and the corresponding key-value pair in the WeakMap will disappear automatically, and there is no need to manually delete the reference.

2: Characteristics of WeakMap

Only accept objects as keys (except null), and do not accept other types of values ​​as keys

not traversable

Because WeakMap has a weak reference relationship to the object referenced by the key name, the internal members of WeakMap will depend on whether the garbage collection mechanism is executed. The number of members may be different before and after running, and the execution of the garbage collection mechanism is impossible. Predictable and therefore not traversable.

The difference between Map and WeakMap

  • The key of Map can be of any type. WeakMap only accepts objects as keys, and does not accept other types of values ​​as keys.
  • The key of Map is actually bound to the memory address. As long as the memory address is different, it is regarded as two keys; the key of WeakMap is a weak reference, and the object pointed to by the key can be garbage collected. At this time, the key is invalid .
  • Map can be traversed, WeakMap cannot be traversed

Guess you like

Origin blog.csdn.net/asfasfaf122/article/details/128584265