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
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
  • 1,834
  • 1
  • 16
  • 16
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…
Hugo
  • 430
  • 4
  • 9
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
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
  • 14,160
  • 14
  • 66
  • 120
1
vote
0 answers

How to implement an ES6 WeakMap polyfill (compared to Java)

First, what I really want behind this question is to know if it would be possible to implement a reliable SoftMap in Javascript. Babel, or the Memoizee lib seems to provide polyfills for ES6 WeakMap/Set. I've tried to read the code but I'm not…
Sebastien Lorber
  • 79,294
  • 59
  • 260
  • 386
1
vote
2 answers

Need WeakMap (a, b) -> c

I have a memoized fn, where the fn is memoized by two input references: let NewRefCursor = memoized( (deref, swap) => refToHash(deref) + refToHash(swap), // memoizer identity fn (deref, swap) => new RefCursor(deref, swap)); // the function…
Dustin Getz
  • 19,902
  • 13
  • 77
  • 127
1
vote
1 answer

Not able to understand the behavior of WeakMap in ECMA6

I am facing a weird scenario while playing with WeakMap in ECMA6. I am writing a class which is as follows 'use strict'; class WeekMaptest { constructor(options){ console.log("constructor"); this.weekMap = new WeakMap(); …
Rohit Choudhary
  • 2,191
  • 1
  • 21
  • 33
0
votes
1 answer

How to update a value of a existing key in WeakMap?

I can't find any example on MDN. I came up with this, const simpleMap = new WeakMap() const simpleObject = {}; simpleMap.set(simpleObject, "A Value"); if(simpleMap.has(simpleObject)) { simpleMap.set(simpleObject, "A New Value"); } Is this…
jeffbRTC
  • 1,701
  • 3
  • 16
0
votes
0 answers

JavaScript weak maps - shouldn't it be showing empty?

As I understand it, with JavaScript weak maps, when an object used as a key has no remaining references to it, it is garbage collected (or at least slated for garbage collection) and removed from the weak map. Quoting from javascript.info (emphasis…
Mitya
  • 30,438
  • 7
  • 46
  • 79
0
votes
1 answer

WeakMap with event.target

Edit: turns out nothing is actually wrong with the second snippet (my real code). On one page it works, and on another it doesn't. Yea for underlying errors. I'm creating a DOM element and giving that DOM element to a WeakMap as a key. Then, with…
acw
  • 503
  • 1
  • 9
0
votes
1 answer

Are same key WeakMaps blocking?

So WeakMap is weak over the keys... what if I would have two WeakMaps that both store DOM Elements as key with some different values. Let's assume, that we cannot combine them... If we would remove the DOM Element from the DOM, that would mean that…
Fabian
  • 79
  • 2
  • 8
0
votes
0 answers

Getting and empty string with different reference than all other empty strings

I need to be able to create a copy of strings in C# so that they are not equal in reference to the original string. As per Microsoft's documentation, this can be achieved by calling this code: public static string GetNewString(string oldString) { …
gulu
  • 45
  • 6
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 …
0
votes
3 answers

How to observe garbage collection of a WeakMap in Javascript?

It is my understanding of WeakMap that "References to objects in the collection are held weakly. If there is no other reference to an object stored in the WeakMap, they can be garbage collected." Why do the following key/value pairs still appear in…
dandan
  • 666
  • 4
  • 13