TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

All About Recursion and Tail Calls in JavaScript

67 pointsby blacksmytheabout 8 years ago

5 comments

xg15about 8 years ago
What I don&#x27;t understand with STCs is, how would I, as a developer, decide when to use them and when not?<p>As program correctness is concerned, tail calls and recursive calls should behave exactly the same (except stack use), so going by correctness alone, either the choice doesn&#x27;t matter at all or tail calls are preferable. So the logical thing to do would be to always use tail calls.<p>The post goes on to list a number of disadvantages, but those seem to be either debugging concerns or platform-specific implementation concerns.<p>The former doesn&#x27;t seem to be something that should be solved in the code - the document itself lists a number of solutions. The latter is not something that I as developer could judge as I likely don&#x27;t have knowledge about the specific implementation of the platform the code is running on (if I know the platform in advance at all)<p>So it seems to me, STC shifts the problem of deciding to the developer even though the developer has no good tools to actually solve it.
评论 #14333652 未加载
评论 #14333705 未加载
holmberdabout 8 years ago
Until ES6 which introduces Tail Call Optimization, you can use trampolines.<p>Small summary code: <a href="http:&#x2F;&#x2F;paste.ubuntu.com&#x2F;24568118&#x2F;" rel="nofollow">http:&#x2F;&#x2F;paste.ubuntu.com&#x2F;24568118&#x2F;</a>
评论 #14331637 未加载
Kholoabout 8 years ago
What&#x27;s the best way to benchmark perf optimizations in JavaScript. I see lot of articles like this with no time and memory usage stats. I would like to hear an answer from someone who has experience using gdb or visual studio while studying performance. Whenever I use the chrome debugger to time anything involving async calls and recursion I have this uncomfortable feeling I am not getting it right. With gdb or visual studio C&#x2F;C# I always feel like I know what is going on.
评论 #14331975 未加载
评论 #14330962 未加载
评论 #14331600 未加载
评论 #14331554 未加载
cpetersoabout 8 years ago
JavaScript TCO can&#x27;t be feature detected so how do you know when tail calls are safe to use in front end or library code that can run on a variety of clients?
评论 #14333710 未加载
wcummingsabout 8 years ago
Which js implementations currently support TCO?
评论 #14332525 未加载