JavaScript Tail Call Optimization

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • only return call() either implicitly such as in arrow function or explicitly, can be a tail call statment
  • function foo(){ return bar(); } // the call to bar is a tail call
  • function foo(){ bar(); }// bar is not a tail call. The function returns undefined when no return is given
  • const foo = () => bar(); // bar() is a tail call
  • const foo = () => (poo(),bar()); // poo is not a tail call, bar is a tail call
  • const foo = () => poo() && bar(); // poo is not a tail call, bar is a tail call
  • const foo = () => bar() + 1; // bar is not a tail call as it requires context to return + 1

Remarks

TCO is also known as PTC (Proper Tail Call) as it is referred to in the ES2015 specifications.



Got any JavaScript Question?