❯ 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
Last active
March 13, 2019 11:19
-
-
Save ricardobcl/3dfac3d1b3c345d49962787b3fc02855 to your computer and use it in GitHub Desktop.
Async/await overhead
This file contains hidden or 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
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment