Last active
November 5, 2022 22:52
-
-
Save coder-mike/5c661d061baccdb149283f25d7ebb2c9 to your computer and use it in GitHub Desktop.
Measure cost of async in node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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