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

161
Vistas
How long does a static class member stay in memory in javascript?

Say I have a class like so:

class KeyStorage {
  // unrelated functionality omitted 
  static Keys = {}; // contains a bunch of "key" related objects
  static #foundKeys = {};

  static #getKey(name, ensureValid = true) {
    let foundKey = this.#foundKeys[name];
    if (foundKey && foundKey.key.id === name) {
      this.#log("getKey", name, foundKey, "FOUND IN HASH TABLE");
      this.#foundKeys[name].requestCount++;
      return foundKey;
    }
    for (let storageKey in KeyStorage.Keys) {
      const key = KeyStorage.Keys[storageKey];
      if (key.id === name) {
        foundKey = key;
        this.#log("getKey", name, foundKey, "FOUND IN LOOP");
        break;
      }
    }
    if (ensureValid) ArgumentNullError.Guard("foundKey", foundKey);
    this.#foundKeys[name] = { key: foundKey, requestCount: 1 };
    return foundKey;
  }
}

As you can see, when looking for a key I first check the #foundKeys property to see if I've already looked for it before, if not I look for it then add it to the #foundKeys object at the end if so. My question is, how long does the application keep that object in memory?

The class is heavily used so I can't see a reason garbage collection would kick in while the application is still accessing the object, but I haven't found any info relating to this. Is "caching" non-sensitive and relatively unimportant data in this way a bad practice?

In the case of this class, it isn't massively important I keep the object alive for the entire life of the application, it's just to boost performance by avoiding constantly looping for a key if I've already found it recently, but it'd be good to know when to avoid doing this as I've considered the same approach for other issues but I'm not sure what the risks would be, if any.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

How long does a static class member stay in memory in javascript?

As long as the class itself stays in memory. Which usually is the entire lifetime of the application.

about 4 years ago · Santiago Trujillo 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