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
3
votes
1 answer

Php's "new" WeakMap - do Enums ever get garbage collected?

https://www.php.net/manual/en/class.weakmap.php what about Enums? You can do enum MyEnum{ case A; } $wm = new WeakMap(); $wm[MyEnum::A] = 'something'; when does $wm[MyEnum::A] become invisible? or inaccessible? if ever? I am NOT talking about $wm.…
Toskan
  • 13,911
  • 14
  • 95
  • 185
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){ …
Muhammad Rizwan
  • 184
  • 2
  • 4
3
votes
3 answers

How to new WeakMap with array as parameter?

I have been reading MDN docs about WeakMap. And it mentions the syntax: new WeakMap([iterable]) But when I tried this, error occurred: var arr = [{a:1}]; var wm1 = new WeakMap(arr); Uncaught TypeError: Invalid value used as weak map key Could you…
krave
  • 1,629
  • 4
  • 17
  • 36
3
votes
2 answers

WeakMap inverted

Is there a way to create WeakMap of any other weak references in Javascript for storing key value pairs where key is String/Number and value is Object. The referencing would have to work something like this: const wMap = new WeakRefMap(); const…
Nabuska
  • 443
  • 9
  • 17
2
votes
1 answer

IE8 compliant weakmap for DOM node references

I want to have literally a Dictionary This is basically an ES6 WeakMap but I need to work with IE8. The main feature I want is minimize memory leaks O(1) lookup on Object given Node. My implementation: var uuid = 0, …
Raynos
  • 166,823
  • 56
  • 351
  • 396
2
votes
2 answers

Why are WeakRef polyfills made with WeakMap? I don't see how that can work

If you look at both of these examples of WeakRef polyfills, they both use WeakMap. But I don't see how that can work. A WeakMap doesn't hold weak references to its values, but to its keys. And both those polyfills use this as the key. Which means if…
Evert
  • 2,022
  • 1
  • 20
  • 29
2
votes
1 answer

Multiple private properties in JavaScript with WeakMap

I want to set my class properties private so I used WeakMap but with a single WeakMap. After my items creation, I get only the last object data, previous data are deleted... This is my code definition: const Item = (() => { const weakMap = new…
Nammen8
  • 619
  • 1
  • 11
  • 31
2
votes
1 answer

Experimenting with auto-removed items from WeakSet/WeakMap (via garbage collection) in Node.js when .size doesn't exist?

#1. Workaround for lack of .size property? In JavaScript, I've never used either WeakSet or WeakMap before, and I don't know that much about garbage collection in general (I'm a PHP + JS developer, so this is the first time I've really needed to…
LaVache
  • 2,372
  • 3
  • 24
  • 38
2
votes
1 answer

JavaScript extend a class while using WeakMap for private variables

I wrote some classes that have private variables through the use of WeakMap(). I did this by placing a WeakMap at the top of the class file. light.js let privateVars = new WeakMap(); class Light { constructor(state, brightness) { let info =…
Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
2
votes
1 answer

Using DOM node as key in WeakMap

When working with WeakMap I came across a scenario where I find quite puzzling: let's say I have a DOM node with some data I want to store, and I store it in a WeakMap using the element/node itself as the key, and the arbitrary data as…
Terry
  • 63,248
  • 15
  • 96
  • 118
2
votes
0 answers

What are some good use cases for the JS WeakSet?

Both the JavaScript WeakSet and WeakMap ES2015 standard built-in objects are JS structures with which I have little experience using. I know that the weak part of WeakMap/WeakSet refers to references in those objects being held weakly. But what are…
IsenrichO
  • 4,251
  • 3
  • 18
  • 31
2
votes
1 answer

Creating WeakMap wrapper implementation in GWT -- getting errors

I have been recently looking at the upcoming js (harmony) weakmap support that would solve a number of complex problems I currently have. Luckly there is a shim (https://github.com/Benvie/WeakMap) with pretty good browser support which led me to…
Casey Jordan
  • 1,204
  • 1
  • 20
  • 39
1
vote
0 answers

What is the difference between using a Weak Map vs an Object with a closure to simulate private properties for a class?

Trying to wrap my head around this. Google Apps Script does not yet support private properties in a class, as alternative many posts suggest the use of Weak Map and a closure to approximate this behavior. Like this: const myPrivate1 = (() => { …
1
vote
2 answers

How do I clone a “WeakMap” or “WeakSet” in Javascript?

I know that WeakMap and WeakSet are not iterable for security reasons, that is, “to prevent attackers from seeing the garbage collector’s internal behavior,” but then, this means you cannot clone a WeakMap or WeakSet the way you clone a Map or Set,…
1
vote
0 answers

Does cross-referencing in WeakMap prevent garbage collection?

I'm extending this question: Would a "circular" reference be treated as "reachability" for a WeakMap? If I have 2 WeakMaps that have entries whose values cross references the keys, would this prevent garbage collection or not? Example: let obj1…