This function should be tail call optimized.
To my knowledge, current browsers (Chrome, even tried it on Canary) should optimize it yet I get an error for this run:
function die(x, s) {
return x === 0 ? s : die(x-1, s+1);
}
die(100000, 0);
The error:
VM369:1 Uncaught RangeError: Maximum call stack size exceeded
Or did I get something wrong?
Solved it within 5 minutes of posting, it might be interesting to learn so I'll post the answer:
Tail calls are optimized in strict mode only, so this works: (If running in chrome make sure experimental Javascript is enabled under chrome://flags
)
(function () {
"use strict";
function die(x, s = 0) {
return x === 0 ? s : die(x -1, s + 1);
}
return die(100000);
})();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With