raganwald.github.com
raganwald.github.com copied to clipboard
tail call length still hits call stack
Hi,
I was reading http://raganwald.com/2015/02/07/tail-calls-defult-arguments-recycling.html, and it's fascinating stuff, thank you.
However, I was doing some testing myself, and I found that the tail call version of length()
still hits Maximum call stack size exceeded
at ~ 8,400 item long arrays on my Macbook Pro
Is this because of the inefficiency of [first, ...rest]
. My understanding of the article was that the recursion was now ok because JavaScript optimizes away the function call overhead and stack space.
I'm just finding both the tail call length
and the inefficient one fall over at the same point:
const length = ([first, ...rest], numberToBeAdded = 0) =>
first === undefined
? numberToBeAdded
: length(rest, 1 + numberToBeAdded)