如何在 ES5/strict 模式下启用正确的尾调用?

How are proper tail calls enabled in ES5/strict mode?

今天,我正在阅读harmony:proper_tail_calls proposal and I noticed that in the references there was a link which read, “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/strict 模式下“启用”正确的尾调用?

这意味着严格模式使得在适当的尾部位置的调用保证能够作为尾部调用实现,因为它禁止任何可能干扰该优化的事情;即不能通过 caller 属性 访问严格模式函数。在 Firefox 已经实现它的意义上,它并不意味着“启用”。