Tail call optimization
Explore how ES6 JavaScript handles tail call optimization to improve recursive function efficiency. Understand the role of the call stack and how optimized recursion can reduce memory complexity. This lesson guides you through practical examples and current browser support for this feature in ES6.
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:
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) ...