是否有任何 JavaScript 引擎尾调用 (TCO) 优化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3660577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are any JavaScript engines tail call (TCO) optimized?
提问by clofresh
I have a tail recursive pathfinding algorithm that I've implemented in JavaScript and would like to know if any (all?) browsers would possibly get stack overflow exceptions.
我有一个用 JavaScript 实现的尾递归寻路算法,想知道是否有任何(所有?)浏览器可能会出现堆栈溢出异常。
采纳答案by Tim Sylvester
The ECMAScript 4 specification was originally going to add support for TCO, but it was dropped:
ECMAScript 4 规范原本打算添加对 TCO 的支持,但它被删除了:
No more tail calls in JavaScript?
As far as I know, no widely-available implementations of JavaScript currently do automatic TCO. This may be of use to you, though:
据我所知,目前还没有广泛使用的 JavaScript 实现可以实现自动 TCO。不过,这可能对您有用:
Essentially, using the accumulator pattern accomplish the same effect.
本质上,使用累加器模式可以实现相同的效果。
回答by Mr Speaker
No joy for the moment, but thankfully proper tail calls are slated for Harmony (ECMAScript version 6) http://wiki.ecmascript.org/doku.php?id=harmony:proper_tail_calls
暂时没有快乐,但幸运的是,Harmony(ECMAScript 版本 6)有适当的尾调用 http://wiki.ecmascript.org/doku.php?id=harmony:proper_tail_calls
回答by Hank Gay
Pretty much every browser you encounter will barf on "too much recursion". Here's an entry in the V8 bug trackerthat will probably be interesting reading.
几乎您遇到的每个浏览器都会对“太多递归”感到厌烦。这是V8 错误跟踪器中的一个条目,可能会很有趣。
If it's simple self-recursion, it's probably worth the effort to use explicit iteration rather than hoping for tail-call elimination.
如果它是简单的自递归,那么使用显式迭代而不是希望消除尾调用可能是值得的。
回答by Simon Zyx
Tail call optimization will be supported In ECMAScript 6 strict mode in the future. Check http://www.2ality.com/2015/06/tail-call-optimization.htmlfor details.
未来将在 ECMAScript 6 严格模式中支持尾调用优化。查看http://www.2ality.com/2015/06/tail-call-optimization.html了解详情。
Check http://kangax.github.io/compat-table/es6/for current engine support.
检查http://kangax.github.io/compat-table/es6/以获得当前的引擎支持。
At the moment (18-07-2019) the following engines support tail call optimization:
目前(18-07-2019)以下引擎支持尾调用优化:
- Safari >= 10
- iOS >= 10
- Kinoma XS6
- Duktape 2.3
- Safari >= 10
- iOS >= 10
- 奇诺玛XS6
- 杜克磁带 2.3
support if "experimental JavaScript features"-flag is turned on:
如果打开“实验性 JavaScript 功能”标志,则支持:
- Node 6.5
Chrome 54 / Opera 41Current version of the compat table does not list it anymore
- 节点 6.5
Chrome 54 / Opera 41当前版本的兼容性表不再列出
回答by Santosh
Tail call optimization is now available in LispyScriptwhich compiles to JavaScript. You can read more about it here.
现在可以在编译为 JavaScript 的LispyScript中使用尾调用优化。您可以在此处阅读更多相关信息。
回答by mcfedr
Currently no JavaScript implementations recognise tail recursion. Changes are being made in ECMAScript 6, and as others have said, there is an open ticket on V8.
目前没有 JavaScript 实现识别尾递归。ECMAScript 6正在发生变化,正如其他人所说,V8上有一张公开票。
Here you can see V8's generated assembler for a tail recursion function:
在这里你可以看到 V8 为尾递归函数生成的汇编程序:
Example of how V8 compiles recursion
Compare that to how Clanghas compiled the same function in C
将其与Clang如何在 C 中编译相同的函数进行比较
Example of C compiler tail recursion
V8 retains the recursive call, whereas the C compiler has recognised the tail recursion and changed it into a loop.
V8 保留递归调用,而 C 编译器已识别尾递归并将其更改为循环。

