Created
September 3, 2014 21:09
-
-
Save zenparsing/df4be65d51265d4976c2 to your computer and use it in GitHub Desktop.
Await Performance
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 queueAlways() { | |
let x; | |
for (let i = 0; i < 100000; ++i) | |
x = await i; | |
} | |
async function queueIf() { | |
let x; | |
for (let i = 0; i < 100000; ++i) | |
x = ("then" in Object(i) ? await i : i); | |
} | |
async function time(fn) { | |
let ts = +new Date; | |
await fn(); | |
return (+new Date) - ts; | |
} | |
export async function main() { | |
let time1 = await time(queueAlways), | |
time2 = await time(queueIf); | |
console.log(`Conditional: ${ time2 }ms`); | |
console.log(`Always: ${ time1 }ms (${ (time1 / time2).toFixed(0) }x slower)`); | |
} | |
/* | |
Running with es6now 0.8.10 (Node v0.11.13 --harmony) | |
Conditional: 35ms | |
Always: 941ms (27x slower) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment