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

Accessing private variables defined with WeakMap inside a derived class

I'm using the common WeakMaps pattern to emulate private variables inside es6 classes, but I cannot find a way to have "protected" variables, meaning variables that are private and that can be accessed through derived classes, eg: var Window =…
Row Rebel
  • 237
  • 3
  • 10
0
votes
1 answer

Javascript Flyweight with WeakMap or WeakSet

I want a Flyweight object so I created an Object and stored it's instances in a Map like this: const FlyweightNumber = (function(){ "use strict"; const instances = new Map(); class FlyweightNumber{ constructor(number){ …
0
votes
2 answers

How can a weak map be implemented in ES5?

There is another question that asks the same, but I cannot grok the accepted answer. The library in question appears to use Object.defineProperty to add a reference to the object to be stored (albeit indirectly via another object). But... surely…
Ben Aston
  • 45,997
  • 54
  • 176
  • 303
0
votes
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,612
  • 3
  • 33
  • 45
0
votes
1 answer

Will WeakMap save me from memory leak for parent / child relationship?

I have a parent / child relationship that looks like this. parent.children = [child]; child.parent = parent; Will this cause memory leak, when all other references to parent and child are removed? Only references that are remaining will be by each…
Joon
  • 7,348
  • 5
  • 41
  • 64
0
votes
1 answer

Timing issues considerations when using WeakMap from EcmaScript

What is the proper usage of the WeakMap in JavaScript? What kind of timing issues may occur when I use it? IN particular, I am wondering what would happen in the following situation: var wm1 = new WeakMap() var o1 = {}, o2 = function(){}, o3 =…
vmg
  • 8,856
  • 12
  • 55
  • 83
-2
votes
1 answer

WeakMap in Javascript

Can you clarify why boolean is used while adding objects to the WeakMaps in the code below. I understand set takes two(key and value) arguments. The boolean values gets printed in the console as well…that is my doubt… Thanks in Advance. const book1…
praveen-me
  • 438
  • 3
  • 10
1 2 3
4