DEV Community

Discussion on: Recursion, Tail Call Optimization and Recursion.

Collapse
 
qm3ster profile image
Mihail Malo • Edited

ES6 spec mandated TCO for mutually recursive functions as well that both call each other in tail call position.
Only Apple browsers have it implemented at the moment, Chrome implemented it but later removed it.

One notable part of the spec was that

function a(n){
  if (n<=0) return
  a(n-1)
}

would not get optimized, because there is an implicit return undefined after the last line.
I understand how that limitation came about, but I don't really see why they couldn't add a single "void" to the stack and then merrily recurse as if it's this:

function a(n){
  if (n<=0) return
  return a(n-1)
}

since the return value of the inner a is never used.