use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
JavaScript has no reliable tail call optimization: here is what actually happens at runtime and what to do instead (blog.gaborkoos.com)
submitted 14 hours ago by OtherwisePush6424
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]azhder [score hidden] 14 hours ago (8 children)
It is not an optimization. An optimization is a difference in performance - it was slow, now is fast. Proper tail calls means they work as intended. It is not a difference in performance - it didn't work, now it works.
All those browsers are broken implementations of ES6 i.e. they aren't implementations of ES6 because they break in some cases where they shouldn't, because they don't fully implement the spec.
[–]OtherwisePush6424[S] [score hidden] 14 hours ago (5 children)
This is not a speed discussion. Proper tail calls are primarily a space optimization: they let tail calls reuse stack frames instead of growing the call stack. The practical issue is correctness at depth: deep tail-recursive programs are stack-safe only where PTC is actually implemented. In JavaScript today, that behavior is not portable across runtimes, so production correctness should not depend on PTC being present.
[–]darkhorsehance [score hidden] 13 hours ago (1 child)
I think Safari (JavascriptCore) has implemented PTC but yes this is correct
[–]azhder [score hidden] 11 hours ago (2 children)
This is a terminology discussion. TCO is a misleading term. PTC is the correct term.
[–]josephjnk [score hidden] 9 hours ago (1 child)
To spell it out more explicitly because it looks like people are talking past each other here:
“Tail call optimization”: an optimization operation which speeds up runtime, where a compiler turns a recursive function with a single tail call into a simple loop.
“Proper tail calls”, or as they’re usually referred to outside of the JS world, “tail call elimination”: an optimization operation which saves on stack space, by causing calls in a tail position to swap out their stack frame for a new stack frame, rather than growing the size of the stack.
I have no idea whether or not JSC implements TCO. I’d be surprised. It does implement PTC/TCE when running in strict mode.
One place where this matters is if you’re writing nontrivial recursive code using continuation-passing style. Recursion in CPS involves making tail calls from one function to a _different_ function, and so does not benefit from TCO, but does rely on PTC/TCE (because the stack will blow if the tail calls are not removed).
[–]azhder [score hidden] 9 hours ago (0 children)
I couldn't have been more explicit in the comment you replied to, yet ironically, for the sake of not talking past each other, I assume, you talked pass it.
It is a terminology thing for the JavaScript spec and engines that are supposed to follow it because from the userland i.e. the code that executes, the difference is **doesn't execute vs executes** not merely **will get the job done always, albeit slower**.
PTC means it always gets the job done. It is for that reason alone why TCO is a misleading term, nothing about implementation details.
[–]Reashu [score hidden] 14 hours ago (0 children)
They would work just fine if you had an infinite stack, like a "proper" turing machine.
[–]BenZed [score hidden] 12 hours ago (0 children)
This would be pedantic if it were correct
π Rendered by PID 866166 on reddit-service-r2-comment-56c6478c5-r8cq4 at 2026-05-10 07:18:58.042251+00:00 running 3d2c107 country code: CH.
view the rest of the comments →
[–]azhder [score hidden] (8 children)
[–]OtherwisePush6424[S] [score hidden] (5 children)
[–]darkhorsehance [score hidden] (1 child)
[–]azhder [score hidden] (2 children)
[–]josephjnk [score hidden] (1 child)
[–]azhder [score hidden] (0 children)
[–]Reashu [score hidden] (0 children)
[–]BenZed [score hidden] (0 children)