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

68 questions
515
votes
8 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
  • 8,365
  • 4
  • 22
  • 29
112
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,855
  • 4
  • 31
  • 40
35
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
  • 3,340
  • 5
  • 30
  • 42
34
votes
3 answers

How to iterate over a WeakMap?

A JavaScript 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 not .. how does the Chrome console do this ?
commonpike
  • 10,499
  • 4
  • 65
  • 58
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
  • 98,741
  • 129
  • 357
  • 507
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…
12
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
  • 16,489
  • 37
  • 131
  • 266
11
votes
2 answers

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 =…
Bruno Mazza
  • 675
  • 1
  • 10
  • 24
11
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
  • 249,484
  • 69
  • 436
  • 539
10
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
  • 37,288
  • 33
  • 152
  • 232
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
  • 112,777
  • 145
  • 353
  • 547
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
  • 16,006
  • 16
  • 82
  • 141
4
votes
3 answers

How to observe garbage collection of a WeakMap in Javascript?

It is my understanding of WeakMap that "References to objects in the collection are held weakly. If there is no other reference to an object stored in the WeakMap, they can be garbage collected." Why do the following key/value pairs still appear in…
dandan
  • 1,016
  • 8
  • 16
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
  • 118,113
  • 30
  • 216
  • 245
1
2 3 4 5