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

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
  • 381
  • 8
  • 16
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
  • 156,883
  • 55
  • 337
  • 385
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…
Hazlo8
  • 565
  • 8
  • 27
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
  • 1,587
  • 2
  • 17
  • 27
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
  • 17,347
  • 30
  • 101
  • 195
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
  • 48,492
  • 9
  • 72
  • 91
2
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,059
  • 2
  • 11
  • 26
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
  • 3,193
  • 3
  • 14
  • 30
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,104
  • 1
  • 18
  • 35
1
vote
1 answer

WeakMap showing different results for same code

I was learning about use-cases of weakMaps, weakSets and weakRefs. I came across a code which was written like this: { const x = { a: [1, 2] }; var weakMap = new WeakMap(); weakMap.set(x, 'something'); } console.log(weakMap); Note:…
1
vote
1 answer

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
  • 1,482
  • 1
  • 13
  • 21
1
vote
1 answer

Truly Weak Reference Event Emitter / Dispatcher: is it possible?

I was wondering if truly weak reference event dispatcher / emitter mechanism is possible to be implemented in JS at the current level of development of the JS technology? Yesterday I took a very popular eventemitter3 lib and changed it a bit to use…
Mark Dolbyrev
  • 1,541
  • 1
  • 12
  • 20
1
vote
1 answer

Questions about WeakMap and private variables

In the book I am reading at the moment, it talks about how we can use WeakMap to enforce privacy with the example code below. const Car = (function() { const carProps = new WeakMap(); class Car { constructor(make, model) { this.make =…
Andy Min
  • 71
  • 6
1
vote
3 answers

javascript weakmap keep refrence to deleted object

when delete the object , weakmap keeps refrence to it. but the normal behaviour is : when oyu delete the object it will removed from weakmap automatically and weakmap cannot cause memory leak. is it something wrong with weakmap or delete ? let a = …
1
vote
1 answer

JavaScript Classes - Making variables private with WeakMap and still using "this" in other methods

I am reading through the book "Learning JS DataStructs and Algorithms", and in the book it says that the "items" is public in the following class. class Stack { constructor(){ this.items = [] } } But, if I use a WeakMap then I can…
brff19
  • 402
  • 5
  • 14