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
0
votes
0 answers

JavaScript: If a single object is used as a key in multiple WeakMaps, will garbage collection be prevented?

I have a list of tokens storing the contents of an editor. Each node in the list has a map of dom nodes. Why a map? Because the same block of text could appear in multiple places in the page and I want all instances kept in sync by the same data…
theSherwood
  • 1
  • 1
  • 2
0
votes
1 answer

Do we change states when setting new values in weakmaps?

Suppose we've the following code: const _timeStamp = new WeakMap(); const _running = new WeakMap(); export class Stopwatch { constructor() { _timeStamp.set(this, 0); _running.set(this, false); } start() { // start stopwatch …
PieterT2000
  • 304
  • 2
  • 12
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
  • 267
  • 3
  • 11
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){ …
Paulo Henrique
  • 938
  • 1
  • 13
  • 19
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
  • 53,718
  • 65
  • 205
  • 331
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
  • 9,346
  • 8
  • 48
  • 75
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
  • 9,920
  • 13
  • 61
  • 90
-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
  • 486
  • 3
  • 10
1 2 3 4
5