Created
July 8, 2017 23:33
-
-
Save hieutdo/9d1f3603d65e7415b9f9ddd59ecb765d to your computer and use it in GitHub Desktop.
Async Fibonacci
This file contains 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 fibo1(n) { | |
if (n === 0 || n === 1) return n; | |
const f1 = await fibo1(n - 1); | |
const f2 = await fibo1(n - 2); | |
return f1 + f2; | |
} | |
async function fibo2(n) { | |
if (n === 0 || n === 1) return n; | |
const [f1, f2] = await Promise.all([fibo2(n - 1), fibo2(n - 2)]); | |
return f1 + f2; | |
} | |
async function go() { | |
const n = 20; | |
console.time('fibo1'); | |
await fibo1(n); | |
console.timeEnd('fibo1'); | |
console.time('fibo2'); | |
await fibo2(n); | |
console.timeEnd('fibo2'); | |
} | |
console.clear(); | |
go(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment