简体   繁体   English

为什么没有javascript引擎支持尾调优化?

[英]Why do no javascript engines support tail call optimization?

I recently learned about tail call optimization in Haskell. 我最近在Haskell中了解了尾部调用优化。 I've learned from the following posts that this is not a feature of javascript: 我从以下帖子中了解到这不是javascript的一个功能:

Is there something inherent to javascript's design that makes tail call optimization especially difficult? 是否有一些固有的javascript设计使得尾调用优化特别困难? Why is this a main feature of a language like haskell, but is only now being discussed as a feature of certain javascript engines? 为什么这是像haskell这样的语言的主要特征,但现在才被讨论作为某些javascript引擎的特性?

Tail call optimisation is supported in JavaScript. JavaScript支持尾调用优化。 No browsers implement it yet but it's coming as the specification (ES2015) is finalized and all environments will have to implement it. 没有浏览器实现它,但它随着规范(ES2015)的最终确定而来,并且所有环境都必须实现它。 Transpilers like BabelJS that translate new JavaScript to old JavaScript already support it and you can use it today. 像BabelJS这样将新JavaScript转换为旧JavaScript的运营商已经支持它,您今天就可以使用它。

The translation Babel makes is pretty simple: Babel的翻译非常简单:

function tcoMe(x){
    if(x === 0) return x;
    return tcoMe(x-1)
}

Is converted to: 转换为:

function tcoMe(_x) {
    var _again = true;

    _function: while (_again) {
        var x = _x;
        _again = false;

        if (x === 0) return x;
        _x = x - 1;
        _again = true;
        continue _function;
    }
}

That is - to a while loop. 那是 - 到一个循环。

As for why it's only newly supported, there wasn't a big need from the community to do so sooner since it's an imperative language with loops so for the vast majority of cases you can write this optimization yourself (unlike in MLs where this is required, as Bergi pointed out). 至于为什么它只是支持的,社区并没有很大的需要这么快就这样做,因为它是一个带循环的命令式语言,所以对于绝大多数情况你可以自己编写这个优化(不像需要这样的MLs) ,正如Bergi指出的那样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM