Are any JavaScript engines tail call (TCO) optimized?

Asked
Active3 hr before
Viewed126 times

5 Answers

javascript
90%

PTC - proper tail call TCO - tail code optimization These 2 terms are not the same. And it is important to understand the difference between them for the discussion ahead.,The proper tails call section, (tail call optimization) is red.,Built on Forem — the open source software that powers DEV and other inclusive communities.,It introduces new syntax and new keyword for tail calls optimizations, namely the continue keyword.

Goals
for ECMAScript 2015 include providing better support
for [...].
Some of its major enhancements include modules, class declarations, [..]
and proper tail calls.
load more v
88%

Let’s assume there is a JavaScript engine that manages function calls by storing local variables and return addresses on a stack. How would such an engine execute the code?,To understand what tail call optimization (TCO) is, we will examine the following piece of code. I’ll first explain how it is executed without TCO and then with TCO.,We can make this happen by implementing the function call in line B differently. Before the call happens, the stack looks as follows.,ECMAScript 6 offers tail call optimization, where you can make some function calls without growing the call stack. This blog post explains how that works and what benefits it brings.

To understand what tail call optimization (TCO) is, we will examine the following piece of code. I’ll first explain how it is executed without TCO and then with TCO.

function id(x) {
   return x; // (A)
}

function f(a) {
   let b = a + 1;
   return id(b); // (B)
}
console.log(f(2)); // (C)
load more v
72%

No more tail calls in JavaScript?,Tail call optimization is now available in LispyScript which compiles to JavaScript. You can read more about it here.,At the moment (18-07-2019) the following engines support tail call optimization:,support if "experimental JavaScript features"-flag is turned on:

65%

Consider this recursive function without TCO optimization,About the uncertain future of TCO, see this answer in stackoverflow: ES6 Tail Recursion Optimization.,This sort of optimization becomes crucial when dealing with recursion, especially if the recursion could have resulted in thousands of stack frames. With TCO, the engine can perform all those calls in a single stack frame.,In other words, tail call optimization means that it is possible to call a function from another function without growing the call stack.

For example, here we have a tail call:

function doA(a) {
   return a;
}

function doB(b) {
   return doA(b + 1); //tail call}function foo() {    return 20 + doB(10); //not tail call}foo(); // 31
load more v
75%

TCO recognises that the call from a() to b() is at the tail of function a() and thus there is no need to push a()'s state onto the frame stack. When b(0) returns rather than returning to a() it returns directly to the global frame. Further optimising by eliminating the intermediate steps.,When b() return to a() a()'s frame is popped from the frame stack. It immediately return to the global frame and thus does not use any of the states save on the stack.,Without TCO the call to a() creates a new frame for that function. When that function calls b() the a()'s frame is pushed onto the frame stack and a new frame is created for function b(),It provides a way to optimise recursive and deeply nested function calls by eliminating the need to push function state onto the global frame stack, and avoiding having to step down through each calling function by returning directly to the initial calling function.

It provides a way to optimise recursive and deeply nested function calls by eliminating the need to push function state onto the global frame stack, and avoiding having to step down through each calling function by returning directly to the initial calling function.

function a() {
   return b(); // 2
}

function b() {
   return 1; // 3
}
a(); // 1

Other "javascript-undefined" queries related to "Are any JavaScript engines tail call (TCO) optimized?"