Why can the tail call optimization in ES6 not be used in strict mode?

looking at the introduction to ES6 written by Ruan Yifeng, I found that there was a contradiction in the content.

As mentioned in

clipboard.png
, tail call optimization is only enabled in strict mode, but I use it to call optimized code to run normally without strict mode, and I do use tail call optimization.

function Fibonacci2 (n , ac1 = 1 , ac2 = 1) {
  if( n <= 1 ) {return ac2};

  return Fibonacci2 (n - 1, ac2, ac1 + ac2);
}

Fibonacci2(100) // 573147844013817200000
Fibonacci2(1000) // 7.0330367711422765e+208
Fibonacci2(10000) // Infinity

I can really run out quickly when I run locally. It doesn"t get stuck. It"s really optimized, but there"s no strict mode on here

.
May.13,2022

I tried that in safari browsers, Fibonacci2 (10000) can be executed normally in non-strict mode, but Fibonacci2 (100000) cannot be executed properly.

you can increase the parameter of Fibonacci2 a little bit.

I guess the call stack size of the JS function varies in different environments, which leads you to think that tail recursive optimization is turned on in non-strict mode.


there should be no contradiction. You can use safari to take a look at call stack. There are only recent calls on the call stack optimized for tail calls, and other calls are grayed out, that is, they have been deleted from call stack.
Note:
1, the current tail call optimization is only valid on safari, even the v8 engine used by chrome or node does not have this optimization;
2, the current tail call optimization is only valid in strict mode;
3, for some new function features of es6, such as functions with default parameters, strict mode cannot be enabled.

therefore, in the current environment, tail call optimization is quite limited.

clipboard.png


browsers will automatically optimize.

Menu