InterviewHubPRO
Section Overview
Q513

Question 513 of 521

Part 8: Modern JavaScript APIs, WeakRef, Proxy & Performance (464 - 521)

98%
JavaScript Internals (521 Qs)•Part 8: Modern JavaScript APIs, WeakRef, Proxy & Performance (464 - 521)

What is tail call optimization and does JavaScript support it?

Tail call optimization (TCO) is a technique where a function call in tail position (the last operation before returning) reuses the current stack frame instead of creating a new one, preventing stack overflow in recursive functions.

Tail call example:

javascript
 // Tail call - last operation is the recursive call
 function factorial(n, acc = 1) {
   if (n <= 1) return acc;
   return factorial(n - 1, n * acc); // Tail call
 }

 // Not a tail call - multiplication happens after the recursive call
 function factorialNonTail(n) {
   if (n <= 1) return 1;
   return n * factorialNonTail(n - 1); // NOT a tail call
 }

JavaScript TCO support:

  • Specified in ES6 (ES2015) but poorly supported
  • Only Safari/JavaScriptCore implements it
  • Chrome V8 and Firefox SpiderMonkey do not support it
  • Most JavaScript engines ignore TCO

Workaround - trampolining:

javascript
 function trampoline(fn) {
   while (typeof fn === 'function') {
     fn = fn();
   }
   return fn;
 }

 function factorial(n, acc = 1) {
   if (n <= 1) return acc;
   return () => factorial(n - 1, n * acc);
 }

 const result = trampoline(() => factorial(100000)); // Won't stack overflow

Workaround - iteration instead of recursion:

javascript
 // Recursive (can cause stack overflow)
 function sumRecursive(arr, index = 0, acc = 0) {
   if (index >= arr.length) return acc;
   return sumRecursive(arr, index + 1, acc + arr[index]);
 }

 // Iterative (safe)
 function sumIterative(arr) {
   let sum = 0;
   for (const num of arr) {
     sum += num;
   }
   return sum;
 }

Checking for TCO:

javascript
 function checkTCO(n) {
   if (n === 0) return true;
   return checkTCO(n - 1);
 }

 try {
   checkTCO(100000);
   console.log('TCO supported');
 } catch (e) {
   if (e instanceof RangeError) {
     console.log('TCO not supported');
   }
 }

Best practice: Don't rely on TCO in JavaScript. Use iteration or trampolining for deep recursion.