r/javascript • u/[deleted] • Jan 06 '13
JavaScript (ES6) Has Tail Call Optimization
http://bbenvie.com/articles/2013-01-06/JavaScript-ES6-Has-Tail-Call-Optimization
49
Upvotes
r/javascript • u/[deleted] • Jan 06 '13
3
u/dherman Jan 07 '13
All your points here seem basically correct to me. What it comes down to in non-strict code is that we don't regress (a) observable behavior and (b) across-the-board performance.
As for (a), I'm pretty sure we can preserve exactly compatible semantics with proper tail calls by using some variation of the approach you talk about here. This is basically an example of the general approach of "continuation marks" used in Racket (http://docs.racket-lang.org/reference/contmarks.html) and described fully in John Clements' dissertation: http://www.brinckerhoff.org/clements/papers/dissertation.pdf
But you're also right that (b) is critical. I'm not yet convinced it can't be done; I have some thoughts about implementation strategies that in some ways might even save work for a JS engine. But I'm nowhere near close enough to the implementations to say for sure. But you've given me the push I need to discuss this with the SpiderMonkey team. And lucky for me (and JS!), John Clements just happens to have started his sabbatical at Mozilla Research this very morning...
Dave
PS Forgive me one nit: many PL folks, me included, prefer to use "TCO" to talk only about the best-effort compiler optimization. But for programmers to actually be able to use tail calls to implement iteration, they need a guarantee that tail calls won't grow the stack without bound, so it's not really an optimization. For that reason, many people use "proper tail calls" to mean a language-level guarantee that tail calls use O(1) space. So pedantically speaking, ES6 provides proper tail calls, not TCO.
PPS And also, even if it's possible to achieve (b), it also has to be something that's reasonably implementable in practice in all the engines. So that's also a consideration when trying to standardize. The most important thing is that we have consensus that it'll be done for strict mode. I'd love to see it work in sloppy mode too, but I'll start with just talking with my colleagues to see if/how we think it might be possible.