JavaScript Hard technical 1 views 1 min read

What is tail call optimization and does JavaScript support it?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of JavaScript conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

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:

     // 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:

     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:

     // 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:

     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.

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?