Skip to content

Instantly share code, notes, and snippets.

@coder-mike
Last active November 5, 2022 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coder-mike/5c661d061baccdb149283f25d7ebb2c9 to your computer and use it in GitHub Desktop.
Save coder-mike/5c661d061baccdb149283f25d7ebb2c9 to your computer and use it in GitHub Desktop.
Measure cost of async in node
// node --expose-gc script.js
global.gc(true);
const before = process.memoryUsage().heapUsed;
const arr = [];
const count = 1 * 1024 * 1024;
const never = new Promise(() => {});
for (let i = 0; i < count; i++) {
arr.push(foo(i));
}
async function foo(i) {
// I want to measure the cost of an unresolved async function
await never;
// Make sure `i` is captured into the async state
console.log(i);
}
global.gc(true);
const after = process.memoryUsage().heapUsed;
// On my machine: "Uses 421 bytes per async function"
console.log(`Uses ${Math.round((after - before) / count)} bytes per async function`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment