Skip to content

Latest commit

 

History

History
17 lines (14 loc) · 411 Bytes

21__Tail_Calls.md

File metadata and controls

17 lines (14 loc) · 411 Bytes
title
Tail Calls

Calls in tail-position are guaranteed to not grow the stack unboundedly. Makes recursive algorithms safe in the face of unbounded inputs.

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

// Stack overflow in most implementations today,
// but safe on arbitrary inputs in ES6
factorial(100000)