Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are ECMAScript 6 WeakMaps?

After reading this description: http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps

I'm trying to get a hang of it, but I do not get the overall picture. What is it all about? It seems to be supported in Firefox 6: http://kangax.github.com/es5-compat-table/non-standard/

like image 998
Tower Avatar asked Jul 21 '11 09:07

Tower


People also ask

What are the actual uses of ES6 WeakMap?

WeakMap is a new Data Structure or Collection introduced in ES6. WeakMaps allows you to store a collection of Key-Value pairs. It adopts the same properties of Map. The Major difference is that keys of WeakMap cannot be a primitive data type.

What is WeakSet and WeakMap?

Summary. WeakMap is Map -like collection that allows only objects as keys and removes them together with associated value once they become inaccessible by other means. WeakSet is Set -like collection that stores only objects and removes them once they become inaccessible by other means.

Why do we use WeakMap?

Developers can use a WeakMap to associate private data to an object, with the following benefits: Compared to a Map , a WeakMap does not hold strong references to the object used as the key, so the metadata shares the same lifetime as the object itself, avoiding memory leaks.


2 Answers

A weak reference is a special object containing an object-pointer, but does not keep that object alive.

One application of weak references are implemented in Weak Maps:

“The experienced JavaScript programmer will notice that this API could be implemented in JavaScript with two arrays (one for keys, one for values) shared by the 4 API methods. Such an implementation would have two main inconveniences. The first one is an O(n) search (n being the number of keys in the map). The second one is a memory leak issue. With manually written maps, the array of keys would keep references to key objects, preventing them from being garbage collected. In native WeakMaps, references to key objects are held “weakly”, which means that they do not prevent garbage collection in case there would be no other reference to the object.” Source

(See also my post when ECMAScript Harmony was first released with Firefox... )

like image 78
Lorenz Lo Sauer Avatar answered Sep 21 '22 06:09

Lorenz Lo Sauer


WeakMap

WeakMaps basically allow you to have a HashTable with a key that isn't a String.

So you can set the key to be, i.e. [1] and then can say Map.get([1])

Example from the MDN:

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

wm1.set(o1, 37);
wm1.set(o2, "azerty");
wm2.set(o1, o2); // a value can be anything, including an object or a function
wm2.set(o3, undefined);
wm2.set(wm1, wm2); // keys and values can be any objects. Even WeakMaps!

wm1.get(o2); // "azerty"
wm2.get(o2); // undefined, because there is no value for o2 on wm2
wm2.get(o3); // undefined, because that is the set value

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

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

The reason for its existance is:

in order to fix a memory leak present in many uses of weak-key tables.

Apparently emulating weakmaps causes memory leaks. I don't know the details of those memory leaks.

like image 20
Raynos Avatar answered Sep 18 '22 06:09

Raynos