Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!