ES6 and Typescript Tutorial - 35 - WeakMaps

preview_player
Показать описание

📱 Follow Codevolution

ES6 | ES2015 | Typescript | ES6 Tutorial | ES2015 Tutorial | Typescript Tutorial | ES6 Tutorial for Beginners | ES2015 Tutorial for Beginners | Typescript tutorial for Beginners
Рекомендации по теме
Комментарии
Автор

A map API could be implemented in JavaScript with two arrays (one for keys, one for values) shared by the four API methods. Setting elements on this map would involve pushing a key and value onto the end of each of those arrays simultaneously. As a result, the indices of the key and value would correspond to both arrays. Getting values from the map would involve iterating through all keys to find a match, then using the index of this match to retrieve the corresponding value from the array of values.

Such an implementation would have two main inconveniences. The first one is an O(n) set and search (n being the number of keys in the map) since both operations must iterate through the list of keys to find a matching value. The second inconvenience is a memory leak because the arrays ensure that references to each key and each value are maintained indefinitely. These references prevent the keys from being garbage collected, even if there are no other references to the object. This would also prevent the corresponding values from being garbage collected.

By contrast, native WeakMaps hold "weak" references to key objects, which means that they do not prevent garbage collection in case there would be no other reference to the key object. This also avoids preventing garbage collection of values in the map. Native WeakMaps can be particularly useful constructs when mapping keys to information about the key that is valuable only if the key has not been garbage collected.

Because of references being weak, WeakMap keys are not enumerable (i.e. there is no method giving you a list of the keys). If they were, the list would depend on the state of garbage collection, introducing non-determinism. If you want to have a list of keys, you should use a Map.

meenatchit
Автор

Use WeakMaps to create private methods and properties in a class. Will not be displayed in prototype. Within public methods you can access the private properties or methods you have stored in WeakMaps. In this scenario, WeakMaps are created outside the class, but used within the class. A WeakMap property can be set to initial value in constructor and changed within public methods. A WeakMap method can be called with a public method. (WeakMap get and set will use 'this' as the object that sets the context for the WeakMap value;

markphillips
Автор

'WeakMap' could be used in the DOM-related API caching solution, letting memory be reclaimed when DOM node is removed from the document. Found this example in 'Practical Modern Javascript' by Nicolas.

googlemani
Автор

Mainly WeakMap is used to restrict the properties of a prototype object to out side.

karthickScenation
Автор

Small example for sensing the difference:

let map = new Map();
for(let i=0;;i++){
map.set({}, {})

console.log("entries added:", i);
}
}

node --max-old-space-size=128 --max-semi-space-size=1 test-map.js
//...FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

Now replace with WeakMap and will run for ever.

jamieoliviee