【问题标题】:How are proper tail calls enabled in ES5/strict mode?如何在 ES5/strict 模式下启用正确的尾调用?
【发布时间】:2015-08-05 05:06:10
【问题描述】:

今天,我正在阅读harmony:proper_tail_calls 提案,我注意到在references 中有一个链接,上面写着“Brendan discovers that ES5/strict enables TCO.

ES5/strict “启用” TCO 是什么意思?起初我认为正确尾调用的初始实现可以在 ES5/strict 模式下使用。但是,这些基准测试显然并非如此:

  1. ES5/strict TCO (n = 10000)。
  2. ES5/strict TCO (n = 1000)。

我在上述基准测试中使用了以下两个函数:

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

function with_tco(x) {
    "use strict";
    if (x === 0) return x;
    return with_tco(x - 1);
}

无论如何,我的问题是:如何在 ES5/严格模式下“启用”正确的尾调用?

【问题讨论】:

    标签: javascript ecmascript-5 strict ecmascript-harmony tail-call-optimization


    【解决方案1】:

    这意味着严格模式在正确的尾部位置进行调用,保证能够作为尾部调用实现,因为它禁止任何可能干扰该优化的东西;即不能通过caller 属性访问严格模式函数。这并不意味着 Firefox 已经实现了它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-13
      • 2011-07-10
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      • 2022-08-18
      相关资源
      最近更新 更多