Questions tagged [weakmap]

A weakmap is a data structure composed of key/value pairs in which the keys are assigned using weak references, which means that the bindings of each pair will be removed once the references to the key itself are removed. if you use a WeakHashMap instead the objects will leave your map as soon as they are no longer used by the rest of your program, which is the desired behavior.

A weakmap object provides automatic dereferencing which is intended to reduce memory leaks and facilitate garbage collection. Unlike a map, it is not enumerable.

References

52 questions
440
votes
7 answers

What are the actual uses of ES6 WeakMap?

What are the actual uses of the WeakMap data structure introduced in ECMAScript 6? Since a key of a weak map creates a strong reference to its corresponding value, ensuring that a value which has been inserted into a weak map will never disappear as…
valderman
  • 7,295
  • 4
  • 17
  • 29
99
votes
7 answers

What's the difference between ES6 Map and WeakMap?

Looking this and this MDN pages it seems like the only difference between Maps and WeakMaps is a missing "size" property for WeakMaps. But is this true? What's the difference between them?
Dmitrii Sorin
  • 3,639
  • 3
  • 28
  • 39
31
votes
2 answers

Why will ES6 WeakMap's not be enumerable?

Before my re-entry in JavaScript (and related) I've done lots of ActionScript 3 and there they had a Dictionary object that had weak keys just like the upcoming WeakMap; but the AS3 version still was enumerable like a regular generic object while…
Bartvds
  • 2,809
  • 3
  • 25
  • 40
24
votes
2 answers

How to iterate over a weakmap?

A javascript WeakMap ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap ) does not allow you to get the key, or the length or size, by design. Is it possible to nevertheless loop over entries in some way ? If…
commonpike
  • 8,507
  • 3
  • 56
  • 56
17
votes
3 answers

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:…
Tower
  • 87,855
  • 117
  • 329
  • 496
16
votes
1 answer

Creating a regular weak-reference in Javascript using WeakMaps

I am trying to do the obvious thing with WeakMaps: I want to create a weak reference. In particular, I want to have a list of event-listeners without that list influencing the life of the listener. So I was very excited to find WeakMaps, until I…
Malvolio
  • 38,966
  • 24
  • 87
  • 125
10
votes
2 answers

Why is WeakMap clear() method deprecated?

I have been working with WeakMaps in JavaScript, and after checking the documentation I realized that the clear method has been deprecated / removed from ECMAScript 6. What is the reason for this? Why force us to do a clear function like: clear()…
Flame_Phoenix
  • 13,032
  • 29
  • 108
  • 219
10
votes
1 answer

Would a "circular" reference be treated as "reachability" for a WeakMap?

function f() { const w = new WeakMap(); const o = {}; w.set(o, { v: o }); return w; } const weakMap = f(); For the given code, would the only weakMap item considered as reachable or not? Hence, will it be garbage collected or not? PS:…
zerkms
  • 230,357
  • 57
  • 408
  • 498
9
votes
4 answers

Understanding weak maps

ECMAScript 6 introduces weak maps, available in Node.JS v0.11.3 with the --harmony flag. Consider the following. let weakMap = WeakMap(); let key = []; let rubbish = 'fish cans'; weakMap.set(key, rubbish); rubbish = 'empty bottle'; // Prints "fish…
Randomblue
  • 98,379
  • 133
  • 328
  • 526
8
votes
1 answer

Using an Element as the key to a Hash in JavaScript

I want to create a hash with DOM elements as keys. This is illustrated by the following code: var hash = {}; var set = function(element, value) { hash[element] = value; }; var get = function(element) { return hash[element]; …
Kevin Sylvestre
  • 34,782
  • 30
  • 138
  • 226
7
votes
1 answer

JavaScript(ES6) WeakMap garbage collection when set an object to null

I've just read that WeakMaps take advantage of garbage collection by working exclusively with objects as keys, and that assigning an object to null is equivalent to delete it: let planet1 = {name: 'Coruscant', city: 'Galactic City'}; let planet2 =…
7
votes
1 answer

JavaScript WeakMap keep referencing gc'ed objects

I am experiencing with JavaScript weakmaps, after trying this code in google chrome developer console, running with --js-flags="--expose-gc", I don't understand why the weakmap keep having a reference to a.b if a is gc'ed. var a = {listener:…
6
votes
2 answers

Garbage collection on Map and WeakMap collections in es6

I was reading the WeakMap's description and it said: 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. From reading…
m0meni
  • 14,160
  • 14
  • 66
  • 120
4
votes
2 answers

Are TemplateObject arrays for tagged template literals weakly referenced by their realm?

while (c) { tag`str0 ${e} str1` } The JavaScript runtime creates a frozen array like Object.freeze(['str0 ', ' str1']) but with an additional .raw property. Is it okay to use that object as a key in a WeakMap to avoid having to redo work based on…
Mike Samuel
  • 109,453
  • 27
  • 204
  • 234
3
votes
1 answer

JavaScript/ES6 property does not use setter when setting the value in the constructor

I have a class with a constructor and a couple of properties. const _id = new WeakMap(); class Product { constructor(Id) { _id.set(this, Id); // set } get Id(){ return _id.get(this); } set Id(value){ …
1
2 3 4