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

129
Vistas
How closure causes js memory leak

Could someone explaine to me why below code does not cause any memory leaks? My understanding is variable originalThing is being used in closure unCalled and theroetically referenced by closure someMethod, hence originalThing should not be garbage collected, but i do not see memory leak at all when i was profiling it in browser.

<script>
var theThing = new Array(1000000000);
var replaceThing = function () {
  var originalThing = theThing;
 
  var unCalled = function () {
    if (originalThing)
      return originalThing
  };
  var obj = {
    longStr: new Array(1000000).join('*'),
    someMethod: function () {}
  };
};
setInterval(replaceThing, 1000);
</script>

But if i changed variable obj to theThing then it would cause memory leak, please see code below:

<script>

var theThing = new Array(1000000000);
var replaceThing = function () {
  var originalThing = theThing;
 
  var unused = function () {
    if (originalThing)
      console.log("Hi")
  };
   theThing = {
    longStr: new Array(1000000).join('*'),
     someMethod: function () {}
  };
 
};
setInterval(replaceThing, 1000);
</script>

So what difference deos it make in terms of memory leak caused by closure?

about 4 years ago · Juan Pablo Isaza
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