Tail Call Optimization of Recursive Function in JavaScript

In recursive functions, each call creates a new stack frame, leading to stack overflow errors for deep nesting. Tail call optimization reuses the same stack.

In recursive functions, every time a function calls itself or another function, a new “context” is created and stored on a “stack” data structure. This stack of contexts keeps track of where the program needs to return after each function call is completed. In traditional recursive functions, each call adds a new context to the stack, which can lead to stack overflow errors if the recursion goes too deep.

Tail call optimization (TCO) is a technique to address this issue. It optimizes certain recursive calls by reusing the current context instead of creating new ones. When a function call is in a “tail position,” meaning it’s the last operation to be performed in that function, TCO allows the engine to replace the current context with the new one, rather than adding a new context to the stack.

For example, let’s take the factorial function. In a traditional recursive version, each recursive call creates a new context, leading to stack growth. In a tail-recursive version, the engine recognizes the tail call and updates the current context, avoiding unnecessary stack growth.

This optimization ensures that the call stack doesn’t grow indefinitely, even for large inputs, preventing stack overflow errors and making the function run more efficiently.

Let’s take an example of non tail recursive function to calculate a factorial of a number:

function factorialNonTailOptimized(n) {
  if (n <= 1) {
    return 1;
  } else {
    return n * factorialNonTailOptimized(n - 1); // Not a tail call due to the multiplication operation
  }
}

Now, let’s calculate the factorial of 5 using this non-tail-recursive function and visualize the call stack:

console.log(factorialNonTailOptimized(5));

Visual representation of the call stack:

factorialNonTailOptimized(5) // Creates a new stack frame
  factorialNonTailOptimized(4) // Creates a new stack frame
    factorialNonTailOptimized(3) // Creates a new stack frame
      factorialNonTailOptimized(2) // Creates a new stack frame
        factorialNonTailOptimized(1) // Base case reached, returns 1
      returns 2 * 1 = 2
    returns 3 * 2 = 6
  returns 4 * 6 = 24
returns 5 * 24 = 120 (Final result)

In this example, each recursive call to factorialNonTailOptimized creates a new stack frame to keep track of the intermediate results. As you can see, the call stack grows as the recursion progresses. When the base case is reached ( n <= 1), each stack frame returns a value, and the intermediate results are multiplied together to obtain the final result (factorial of 5).

Now, let’s move on to the tail-recursive version of the factorial function.

function factorialTailOptimized(n, accumulator = 1) {
  if (n <= 1) {
    return accumulator;
  } else {
    return factorialTailOptimized(n - 1, n * accumulator); // Tail call optimized
  }
}

Let’s calculate the factorial of 5 using this tail-recursive function and visualize the call stack:

console.log(factorialTailOptimized(5));

Visual representation of the call stack (with tail call optimization):

With tail call optimization, each recursive call to factorialTailOptimized does not create a new stack frame. Instead, the current stack frame is reused by updating the function arguments and accumulator with the new values. This process is done iteratively, as opposed to creating new stack frames, which effectively prevents the call stack from growing.

As you can see, the tail-recursive version of the factorial function does not lead to a growing call stack and directly returns the final result (factorial of 5) without encountering stack overflow errors. This is the power of tail call optimization, which improves the efficiency of recursive functions in languages that support this optimization technique.

While tail call optimization is useful technique but not all JavaScript engines support it, and the level of optimization may vary among different implementations. while writing tail-recursive functions can be beneficial from a theoretical perspective, it’s essential to consider real-world browser support and performance implications.

Don't Miss Out! Subscribe to Read Our Latest Blogs.

If you found this blog helpful, share it on social media.

Subscription form (#5)

Pin It on Pinterest

Scroll to Top