Tail call optimization
tail call optimization for writing elegant recursive solutions without the performance tax of ES5
We'll cover the following...
A tail call is a subroutine call performed as the final action of a procedure. That is,
return myFunction()
It is important to understand that ES6 does not introduce new syntax for tail call optimization. It is just a different structure of code to make sure that it is efficient.
Let’s calculate the Fibonacci using recursion:
Press + to interact
function fib(n) {if (n <= 1){return n;} else {return fib(n-1) + fib(n - 2);}}
Let’s view the function calls in the form of a tree:
Notice that none of these have any clue that they are actually a part of a bigger process to calculate fib(5)
...