2 ms·
While there is no tail call optimization, you can trampoline a recursive function call to eliminate issues with blowing the call stack. function trampoline
by malandrew 13y ago
While there is no tail call optimization, you can trampoline a recursive function call to eliminate issues with blowing the call stack.
function trampoline(fn /*, args */) {
var result = fn.apply(fn, _.rest(arguments));
while (typeof result === 'function') {
result = result();
}
return result;
}
I learned this wonderful trick from fogus' functional javascript, which I cannot recommend enough.
- kevingadd 13y agoYeah, but that trampoline function is gonna run like garbage because it contains 3 or 4 different things that completely defeat modern JIT optimizations.