[英]tail call optimization in javascript does not work
Ecmascript6 introduced tail call optimization(TCO),I wrote the next code Ecmascript6引入了尾调用优化(TCO),我编写了下一个代码
'use strict';
var isStrict = (function() { return !this; })();
function add5(a,total=0){
if(a==1)return total+a;
return add5(a-1,total+a);
}
add5(100000);
and run it both in chrome(57.0.2987.133 (64-bit)) 并在chrome中运行它(57.0.2987.133(64位))
<script src="strict.js"></script>
and node(v8.1.0). 和节点(v8.1.0)。 they all print the result alike:
他们都打印结果相似:
isStrict:true
/data/study/dashboards/api-demo/strict.js:4
function add5(a,total=0){
^
RangeError: Maximum call stack size exceeded
at add5 (/data/study/dashboards/api-demo/strict.js:4:14)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
It seems the strict mode is enabled,but tail call optimization does not work,anybody can do me a favor and tell why? 似乎启用了严格模式,但是尾部调用优化不起作用,任何人都可以帮我一个忙,并说明原因?
似乎v8.1.0支持TCO,根据http://node.green/节点版本v7.10.x,v7.5.x,v6.11.x可能有效,并且可以在v7.10.0下找到 - - 我试过的和谐旗帜。
It only works in node with --harmony_tailcalls
even on version 8.1.0
. 它仅适用于带有
--harmony_tailcalls
节点,即使在版本8.1.0
。
function addFive(a, total){
'use strict';
if(a < 1) return total;
return addFive(a-1, total+a);
}
addFive(100000, 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.