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

155
Vistas
Scope of parameter passed to async function
{
  const data = new Uint8Array([0x01]);
  await input_writer.write(data);
}

In the above example, 'data' is passed to an async operation, and then immediately scopes out. What happens to the underlying memory? Does it stay alive until the async function is finished with it?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

At first I thought that data was only garbage collectable at the end of the code block, but it seems that the JavaScript engine is smarter than I thought.

Indeed, when executing this snippet:

class Test {}

(async function() {
  const data = new Test;
  await write(data);
  console.log('never');

  function write(data) {
    return new Promise(() => {});
  }
})();

Then using the "Memory" tab to take a memory snapshot, no Test shows up when searching it.

However, if you try this snippet and take a new snapshot, then the instance shows because this implementation of write passes data to setTimeout which keeps a reference on data for a certain time:

class Test {}

(async function() {
  const data = new Test;
  await write(data);
  console.log('in a long time');

  function write(data) {
    return new Promise(resolve => setTimeout(() => resolve(data), 999999999));
  }
})();

Note: you have to paste it in your console to see it in the memory snapshot.

As a conclusion, if write has no side-effect which saves a reference on data, the JavaScript engine (chromium at least) seems to be smart enough to determine that data won't be used anymore even before the end of the code block, which helps clear the memory as soon as possible.

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