Last active
March 30, 2018 03:32
-
-
Save uinz/5887886aaf343612b8b14486157b02b2 to your computer and use it in GitHub Desktop.
尾递归优化
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
| 'use strict'; | |
| function fibonacci(n) { | |
| if (n <= 1) { | |
| return 1 | |
| }; | |
| return fibonacci(n - 1) + fibonacci(n - 2); | |
| } | |
| function fibonacciTail(n, ac1 = 1, ac2 = 1) { | |
| if (n <= 1) { return ac2 }; | |
| return fibonacci2(n - 1, ac2, ac1 + ac2); | |
| } | |
| fibonacci(1000) // 爆栈 | |
| fibonacciTail(1000) // ok | |
| // *NOTE* 开启了尾递归优化的 Node 下 fibonacci(1000) 会卡死, 没有提示 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment