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

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
  • 89,644
  • 67
  • 288
  • 419
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
  • 21,282
  • 15
  • 82
  • 131
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,253
  • 1
  • 23
  • 34
0
votes
1 answer

Will WeakSet be garbage collected if values are used as keys in Map?

I have some HTML elemenets that are used as keys in ES6 Maps, I also have a "WeakSet" which contains those same HTML elements as values, now those HTML elements will eventually be removed from DOM by some other functionality, will then they be…
0
votes
1 answer

How to convert a WeakMap to a JSON Object in Javascript?

Given a JS WeakMap, I want to convert this to JSON data. const weakmap = new WeakMap() // Convert weakmap to JSON data / JS object I've tried JSON.parse(JSON.stringify(weakmap)) which returns empty object. Since one cannot iterate over keys of…
Ayush Jain
  • 316
  • 4
  • 11
0
votes
0 answers

How to set GUI options for three.js globe texture? Weakmap error

I've added gui options for my globe to change textures but I get the error "TypeError: Attempted to set a non-object key in a WeakMap" I think the error is related to the textures I'm using. what can I do to fix the gui error? here is my code: …
shange
  • 77
  • 7
0
votes
0 answers

Should I unsubscribe from private streams defined in components? I tested their destruction with WeakMap

For example, I: Created a class with private Subject and WeakMaps to add Subject and Subscription const subjectsMap = new WeakMap(); const subscriptionsMap = new WeakMap(); class Test { #privateSubject = new…
0
votes
0 answers

what is the memory size of a weakmap object?

For example, Suppose I have a weak map which links some dom element with another dom element like this: let item1=document.querySelector("#item1"); let item2=document.querySelector("#item2"); const wMap = new WeakMap(); wMap.set(item1,item2); This…
cak3_lover
  • 1,440
  • 5
  • 26
0
votes
0 answers

passing directly "object literal param"_{ }_ in WeakMap() works , although no reference to it

I noticed something, when i create an object and set it in WeakMap(), it is stored within WeakMap(), but when i re-assign this variable with value "null", "Grabage collector" remove this key from WeakMap(), because no reference to it. let object1 =…
Anonumouz
  • 62
  • 6
0
votes
0 answers

Why can WeakMap and WeakSet work with 1 element, but cannot work with the whole data collection at once?

I've read that WeakMap and WeakSet don't support working with the entire data collection at once, which means that keys(), values(), entries(), size() and loops are inaccessible for these type of collections. It's written that the reason of that…
Ivan
  • 478
  • 2
  • 13
0
votes
1 answer

Using a WeakMap to track function invocations

In order to learn about using WeakMap, I came up with the following example to track function invocations: var map = new WeakMap(); function countFunctionInvocations(func) { return function(...args) { map.set(func, (map.get(func) ||…
David542
  • 104,438
  • 178
  • 489
  • 842
0
votes
3 answers

Is it possible to implement a two-way weak map in JavaScript?

Is it possible to make something like a WeakMap that is two way (get a value by its key, or get a key by its value)? The usage would look like this (in TypeScript syntax to better illustrate): class TwoWayWeakMap { // What goes here? } class…
trusktr
  • 44,284
  • 53
  • 191
  • 263
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,941
  • 10
  • 29
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
  • 807
  • 3
  • 15
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
  • 119
  • 3
  • 9