Skip to content

Instantly share code, notes, and snippets.

@ricardobcl
Last active March 13, 2019 11:19
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 ricardobcl/3dfac3d1b3c345d49962787b3fc02855 to your computer and use it in GitHub Desktop.
Save ricardobcl/3dfac3d1b3c345d49962787b3fc02855 to your computer and use it in GitHub Desktop.
Async/await overhead
async function f(n) {
if (n === 0 ) return Promise.resolve(1);;
return await f(n-1);
}
async function g(n) {
if (n === 0 ) return Promise.resolve(1);
return g(n-1);
}
function h(n) {
if (n === 0 ) return Promise.resolve(1);
return h(n-1);
}
async function caller(fn) {
for(let i = 0; i<100000; i++) {
await fn(1000);
}
}
async function main() {
let start;
start = new Date();
await caller(f);
console.log(`N async + N await: ${new Date() - start} ms`);
start = new Date();
await caller(g);
console.log(`N async + 1 await: ${new Date() - start} ms`);
start = new Date();
await caller(h);
console.log(`1 async + 1 await: ${new Date() - start} ms`);
}
main();
time node .vscode/async.js
N async + N await: 18228 ms
N async + 1 await: 6064 ms
1 async + 1 await: 434 ms
node .vscode/async.js  25.31s user 1.12s system 105% cpu 25.041 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment