Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda