Subject
- #Recursive Function (재귀함수)
- #Tail Recursion (꼬리 재귀)
- #TCO
Created: 2024-10-12
Created: 2024-10-12 18:47
While studying for a coding test and adding animation effects to text,
while writing a recursive function, I learned about the concept of TCO with the help of AI.
Tail call optimization prevents stack overflow caused by recursive calls and makes memory usage more efficient.
In JavaScript, it is specified to be supported in es6 strict mode, but it seems that not many browsers support it yet.
General recursion
Tail recursion
A method that ends the last operation of a function with a recursive function call is called tail recursion.
It optimizes memory usage because function stack frames are no longer needed!
The way to implement tail recursion in JavaScript is to use the trampoline technique.
In this method, instead of directly returning the result, the factorialTCO function returns a function.
The trampoline function continuously executes these returned functions to obtain the final result.
It is said to be a major technique in functional programming, and it is interesting in that it solves the drawbacks of conventional recursive functions (stack overflow). I actually didn't understand it intuitively just by looking at the code, so I'll have to look it up again if I get stuck.
Comments0