正如里德建议的那样:
function number(n){
return {type:'number',parent:undefined,value:n};
}
function sum(a,b){
return {type:'number',parent:undefined,value:a.value+b.value};
}
var i = number(0);
for (var t=Date.now(); Date.now()-t < 1000;){
var i=sum(i,number(1));}
console.log('Operations with boxing: ',i.value);
var i = 0;
for (var t=Date.now(); Date.now()-t < 1000;){
var i=i+1;}
console.log('Operations without boxing: ',i);
输出:
Operations with boxing: 1911258
Operations without boxing: 16805783
这可能回答了这个问题。我想知道是否有替代方案不会对性能造成如此大的影响。