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
1
vote
0 answers

Invalid value used as weak map key (nextJS & Strapi)

When mapping my fetched data in nextjs it is throwing the error bellow. Data is fetched succesfully, so that's not the problem. The code worked before but when I added new data into strapi it broke.
1
vote
1 answer

Why in WeakMap key and value are not removed when you display the entire collection, but are removed when you display one element?

Example with "nulling" value. Why does the element still exist in the WeakMap collection in this case? let weakMap = new WeakMap(); let obj = {name: 'Ivan'}; //adding some new element weakMap.set({}, obj); obj =…
Ivan
  • 478
  • 2
  • 13
1
vote
2 answers

Two-way WeakMap keeping objects alive?

Assume I have two WeakMaps: a2b = new WeakMap(); b2a = new WeakMap(); If I now do: a2b.set(a, b); b2a.set(b, a); Will this keep both a and b alive or will they be finalized if nobody else is holding on to either a or b?
user3612643
  • 5,096
  • 7
  • 34
  • 55
1
vote
1 answer

How can I make a subclass access private fields from the superclass in javascript?

I am trying to make a stack and queue classes, but I cant make the data field private without not being able to use inheritance. I get an Uncaught SyntaxError: Private field '#data' must be declared in an enclosing class error every time I try. how…
1
vote
0 answers

May I consider values in JavaScript's WeakMap to be weakly reachable?

If I have two WeakMaps in javascript: /** * @type {WeakMap.} */ const providerFtn = new WeakMap(); /** * @type {WeakMap.} */ const provider = new WeakMap(); and I place a Provider and its function in the…
DWoldrich
  • 3,817
  • 1
  • 21
  • 19
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

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,887
  • 1
  • 17
  • 24
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
  • 91
  • 1
  • 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
  • 760
  • 1
  • 8
  • 21
1
vote
1 answer

WeakMap Pattern Singleton without memory leak

class Cat { storage = new Map() constructor(id) { if(storage.has(id)) return storage.get(id) storage.set(id, this) } } I want the object to be removed from the storage if references to it are not used in the application. But if…
Maxmaxmaximus
  • 2,098
  • 1
  • 19
  • 17
1
vote
1 answer

has Map's garbage collection changed recently?

The major difference between Map and WeakMap (as i thought) that: If we have stored an object in Map and then later that object is not referenced to in other places, that object will still not be included in the garbage collection process and we…
user5895938
1
vote
1 answer

Are there any downsides to changing a value of a JavaScript WeakMap key/value pair without using the WeakMap.set method?

I'm just beginning to learn the use cases for the ES6 WeakMap feature. I've read a lot about it, but I haven't been able to find an answer for this particular question. I'm implementing a Node.js Minesweeper game for the terminal - just for fun and…
1
vote
0 answers

Can I store reference on key object from WeakMap value object without preventing GC?

Since GC detects isolated islands of object graph to find candidates for removal from memory and WeakMap does not break island boundaries with it's weak references WeakMap -> KeyObject then it would make sense for this reference WeakMap -> Value ->…
alpav
  • 2,972
  • 3
  • 37
  • 47
1
vote
1 answer

When would you use a Map over a WeakMap when you have objects as keys?

The few times that I've used objects as keys in a map I did it to store metadata about that specific object. I've always used a WeakMap for this because of the benefit of the entry in the map being garbage collected automatically when the object it…
m0meni
  • 16,006
  • 16
  • 82
  • 141