Skip to content

Instantly share code, notes, and snippets.

@souenzzo
Created April 14, 2018 12:32
Show Gist options
  • Save souenzzo/7b5c667cbb278d01c27f2cdcc7335d0a to your computer and use it in GitHub Desktop.
Save souenzzo/7b5c667cbb278d01c27f2cdcc7335d0a to your computer and use it in GitHub Desktop.
Compare async performance with sync.
const fibsync = n => {
if (n <= 1) return 1
const a = fibsync(n - 1)
const b = fibsync(n - 2)
return a + b
}
const fibasync = async (n) => {
if (n <= 1) return 1
const a = fibasync(n - 1)
const b = fibasync(n - 2)
return await a + await b
}
const doTest = async(n) => {
console.log("n = ", n)
console.log("sync start", new Date())
const ressync = fibsync(n)
console.log("sync end", new Date())
console.log("ressync = ", ressync)
console.log("async start", new Date())
const resasync = await fibasync(n)
console.log("async end", new Date())
console.log("resasync = ", resasync)
}
doTest(28)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment