Explanation of JavaScript WeakMap

  • 2021-10-25 06:02:24
  • OfStack

An WeakMap object is a collection of 1 key/value pairs where the key is weakly referenced. The key must be an object, and the value can be arbitrary.

Grammar


new WeakMap([iterable])

Parameter

iterable
Iterable is an array (binary array) or other iterative object whose elements are key-value pairs. Each key-value pair is added to the new WeakMap. null will be treated as undefined.

Describe

key of WeakMap can only be of type Object. The original data type cannot be used as key (such as Symbol).

Why WeakMap?

In JavaScript, map API can be implemented by having its four API methods share two arrays (one for holding keys and one for holding values). Setting a value to this map adds both a key and a value to the end of both arrays. So that the indexes of keys and values correspond in two arrays. When taking a value from the map, you need to traverse all the keys, and then use the index to retrieve the corresponding value from the array of stored values.

However, this implementation will have two major disadvantages. First, the assignment and search operations are the time complexity of O (n is the number of key-value pairs), because both operations need to traverse the whole array for matching. Another drawback is the possibility of a memory leak, because the array will refer directly to each key and value. Such references prevent the garbage collection algorithm from recycling them, even if no other references exist.

In contrast, the native WeakMap holds a "weak reference" to each key object, which means that garbage collection can proceed correctly when no other reference exists. The structure of the native WeakMap is special and efficient, and the key used for mapping is only valid if it is not recycled.

Because of this weak reference, key of WeakMap is not enumerable (there is no way to give all key). If key is enumerable, its list will be affected by the garbage collection mechanism, resulting in uncertain results. Therefore, if you want a list of key values for this type of object, you should use Map.

Basically, if you want to add data to an object without interfering with the garbage collection mechanism, you can use WeakMap.

Attribute

WeakMap.length

The value of the length property is 0.

WeakMap.prototype

Prototype of the WeakMap constructor. Allows you to add attributes to all WeakMap objects.

WeakMap instance

All WeakMap instances inherit from WeakMap. prototype.

Attribute

WeakMap.prototype.constructor
Returns the prototype function that creates an instance of WeakMap. The WeakMap function is the default.

Method

WeakMap.prototype.delete(key)

Removes the associated object of key. WeakMap. prototype. has (key) returns false after execution.

WeakMap.prototype.get(key)

Returns an key associated object, or undefined (without an key associated object).

WeakMap.prototype.has(key)

Returns 1 Boolean value based on whether there is an key associated object.

WeakMap.prototype.set(key, value)

Set a set of key associated objects in WeakMap and return this WeakMap object.

Example

Using WeakMap


const wm1 = new WeakMap(),
   wm2 = new WeakMap(),
   wm3 = new WeakMap();
const o1 = {},
   o2 = function(){},
   o3 = window;

wm1.set(o1, 37);
wm1.set(o2, "azerty");
wm2.set(o1, o2); // value Can be any value , Include 1 Objects or 1 Functions 
wm2.set(o3, undefined);
wm2.set(wm1, wm2); //  Keys and values can be arbitrary objects , Or even another 1 A WeakMap Object 

wm1.get(o2); // "azerty"
wm2.get(o2); // undefined,wm2 Not in o2 This key 
wm2.get(o3); // undefined, The value is undefined

wm1.has(o2); // true
wm2.has(o2); // false
wm2.has(o3); // true ( Even if the value is undefined)

wm3.set(o1, 37);
wm3.get(o1); // 37

wm1.has(o1);  // true
wm1.delete(o1);
wm1.has(o1);  // false

Implement a class WeakMap class with. clear () method


class ClearableWeakMap {
 constructor(init) {
  this._wm = new WeakMap(init)
 }
 clear() {
  this._wm = new WeakMap()
 }
 delete(k) {
  return this._wm.delete(k)
 }
 get(k) {
  return this._wm.get(k)
 }
 has(k) {
  return this._wm.has(k)
 }
 set(k, v) {
  this._wm.set(k, v)
  return this
 }
}

Specification

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
WeakMap
Standard Initial definition.
ECMAScript (ECMA-262)
WeakMap
Living Standard

The above is the JavaScript WeakMap use details, more information about JavaScript WeakMap please pay attention to other related articles on this site!


Related articles: