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

150
Visualizações
JavaScript class field references and garbage collection

I implemented a class that can be used to instantiate objects passed to a visitor function that detects duplicated keys in a JSON document. The problem I am trying to solve is detecting duplicate keys that are in a JSON document. More specifically, I want to detect duplicates at the same nesting level, which I will refer to as context from now on.

The class I implemented has 2 fields:

  • stack: an array that stores all visited keys grouped by context (each item in the array holds keys for a given context)
  • visited: which holds the visited keys for the current context (top of the stack)

My question is the following:

Do I need to clear the Set from the references they might holds to the keys when I pop them out of the stack? In other words, is the discarded?.clear() call necessary as the set holds string references to the visited keys?

I understand that the set reference itself will be lost but I am wondering about the references that the set holds (to the visited keys in the json document)

class DetectDuplicates {
  stack;
  visited;

  constructor() {
    this.stack = [];
    this.visited = new Set();
  }

  onObjectBegin() {
    this.stack.push(new Set());
    this.visitedKeys = this.stack[this.stack.length - 1];
  }

  onObjectEnd() {
    const discarded = this.stack.pop();
    // Is that necessary?
    discarded?.clear();
    this.visitedKeys = this.stack[this.stack.length - 1];
  }

}

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

0

Do I need to clear the Set from the references they might holds to the keys when I pop them out of the stack?

No. If the Set instance is not referenced from anywhere else, it will get garbage-collected, and will take with it all the references stored within it.

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