0

Today, I was reading the 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.

What does it mean that ES5/strict “enables” TCO? At first I thought that initial implementations of proper tail calls were available in ES5/strict mode. However, that is clearly not the case as demonstrated by these benchmarks:

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

I used the following two functions in the above benchmarks:

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);
}

Anyway, my question is: how are proper tail calls “enabled” in ES5/strict mode?

Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299

1 Answers1

2

It means that strict mode makes calls in proper tail position guaranteed to be able to be implemented as tail calls, since it prohibits anything that might interfere with that optimization; namely that a strict-mode function cannot be accessed through a caller property. It doesn’t mean “enables” in the sense that Firefox implemented it already.

Ry-
  • 218,210
  • 55
  • 464
  • 476