Skip to content

Instantly share code, notes, and snippets.

@zenparsing
Created September 3, 2014 21:09
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 zenparsing/df4be65d51265d4976c2 to your computer and use it in GitHub Desktop.
Save zenparsing/df4be65d51265d4976c2 to your computer and use it in GitHub Desktop.
Await Performance
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