DEV Community

DIWAKARKASHYAP
DIWAKARKASHYAP

Posted on

5 1 1 1 1

Tail call optimization in ES6(javascript) in easy language

Tail call optimization (TCO) is a technique used to optimize recursive functions by avoiding the accumulation of stack frames. In ECMAScript 6 (ES6), TCO is not required but some JavaScript engines might provide limited support for it.

Let's understand TCO with an easy example in plain language. Consider a function to calculate the factorial of a number:

function factorial(n) {
  if (n === 0) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
}
Enter fullscreen mode Exit fullscreen mode

In this code, the factorial function recursively calls itself, multiplying the current number n with the factorial of (n - 1). However, this implementation does not utilize TCO.

To enable TCO, we can modify the function to use an accumulator parameter:

function factorial(n, accumulator = 1) {
  if (n === 0) {
    return accumulator;
  } else {
    return factorial(n - 1, n * accumulator);
  }
}
Enter fullscreen mode Exit fullscreen mode

Here, we introduce an extra parameter called accumulator which keeps track of the intermediate result. In each recursive call, instead of directly returning n * factorial(n - 1), we update the accumulator by multiplying it with n and pass it to the next recursive call.

With this modification, the recursive call is now a tail call because it's the last operation before returning, and it doesn't require any further computation. It allows the JavaScript engine to optimize the call by reusing the current stack frame, preventing potential stack overflow errors for large input values of n.

However, it's important to note that ECMAScript 6 doesn't mandate TCO, and its availability depends on the JavaScript engine. Different engines may handle tail calls differently, so the optimization is not guaranteed to work consistently across all environments.

To ensure broader compatibility and reliable optimization, you may consider using iterative approaches or libraries that provide TCO features, such as Babel's babel-plugin-tailcall-optimization or languages like ClojureScript that compile to JavaScript with built-in TCO support.

Thank you for reading this blog, follow me on Twitter, I regularly share blogs and post on Javascript, React, Web development and opensource contribution

Twitter- https://twitter.com/Diwakar_766

Github- https://github.com/DIWAKARKASHYAP

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (2)

Collapse
 
maafaishal profile image
Muhammad A Faishal • Edited

Nice post, btw!

I just found out about TCO for recursive implementation. But, I'm curious about the benchmark with and without TCO. It really helps to emphasize that TCO matters.

Collapse
 
diwakarkashyap profile image
DIWAKARKASHYAP

Thanks for your words! I am glad you find it useful!,

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay