Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

419
Visualizações
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 = null;
console.log(weakMap);
console.log(weakMap.get({}));

Whole collection

enter image description here

One element

enter image description here

Example with "nulling" key:

enter image description here

What's going on here? What does it come from? The garbage collector does not have time to clean up everything or what?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First of all weakMap.set({}, obj); and weakMap.get({}); will never refer to the same element in the WeakMap because they are separate, anonymous object literals. In order for the WeakMap to be useful you need to store a reference to the object to be used as key1.

let weakMap = new WeakMap();
let obj = {name: 'Ivan'};

weakMap.set({}, obj);
console.log('has anonymous key:', weakMap.has({}));
console.log(weakMap.get({}));

let key = {};
weakMap.set(key, obj);
console.log('has assigned key:', weakMap.has(key));
console.log(weakMap.get(key));

Secondly, it is the key that is weakly referenced in the WeakMap, not the value, so setting the value to null will have no effect on the WeakMap. Instead you need to 'null' the key2 (another reason you need a stored reference to it).

let weakMap = new WeakMap();
let obj = { name: 'Ivan' };

let key = {};
weakMap.set(key, obj);

console.log(weakMap);

key = null;

// no good way to indicate this, but debugging in the console will reveal an empty WeakMap pending garbage collection
console.log(weakMap);


1 Your example using an unreachable object as key will disappear from the WeakMap on garbage collection, but not because you set obj=null but because the only reference to the object exists in the WeakMap.

2 There is no good way to demonstrate the behaviour of the second snippet because, per the documentation, '... a WeakMap doesn't allow observing the liveness of its keys, its keys are not enumerable.' Nonetheless, debugging in the console will reveal the behaviour, though awkwardly given the variety of ways different browsers treat objects in the console, ie. chrome's live objects.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda