Wednesday, January 09, 2013

JavaScript (ES6) Has Tail Call Optimization - A Passion for Breaking Things - Brandon Benvie does JavaScript

JavaScript (ES6) Has Tail Call Optimization - A Passion for Breaking Things - Brandon Benvie does JavaScript: "What is a Tail Call and Why Should I Care About Optimizing It?

A tail call is a function call who's return value is used as the caller's return value. Example:

function factorial(remaining, accumulator){
if (remaining === 0) {
return accumulator;
}
return factorial(remaining - 1, remaining * accumulator); // <--- tail call
}
Without tail call optimization, every recursive call will add another frame to the call stack, eventually leading to memory exhaustion. JavaScript engines typically will fail at 10,000 - 30,000 levels of recursion with a RangeError."

'via Blog this'