Created
April 14, 2018 12:32
-
-
Save souenzzo/7b5c667cbb278d01c27f2cdcc7335d0a to your computer and use it in GitHub Desktop.
Compare async performance with sync.
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
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