[英]tail call optimization in javascript does not work
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);
並在chrome中運行它(57.0.2987.133(64位))
<script src="strict.js"></script>
和節點(v8.1.0)。 他們都打印結果相似:
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)
似乎啟用了嚴格模式,但是尾部調用優化不起作用,任何人都可以幫我一個忙,並說明原因?
似乎v8.1.0支持TCO,根據http://node.green/節點版本v7.10.x,v7.5.x,v6.11.x可能有效,並且可以在v7.10.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.