Created
January 6, 2010 00:43
-
-
Save NV/269902 to your computer and use it in GitHub Desktop.
Caching fibonacci function
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
| function fib(n) { | |
| if (n==0) { | |
| return 0 | |
| } else if (n==1) { | |
| return 1 | |
| } else { | |
| return fib[n] = fib[n] || fib(n-2) + fib(n-1) | |
| } | |
| } | |
| console.time('fib') | |
| fib(1000) | |
| console.timeEnd('fib') | |
| // fib: 2ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment