JSFiddle - React, Tailwind, and code Playground
JavaScript
var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];
var expTimes = 100000000, test = 0, start = 0, end = 0, timeA1 = 0, timeB = 0, timeA2 = 0;
//不存入變數1
alert('不存入變數速度測試1開始')
start = Date.now();
for (let i=0;i<expTimes;i++){
test = arr[9];
}
end = Date.now();
timeA1 = (end - start)/1000;
alert('不存入變數速度測試1結束,耗時 ' + timeA1 + '秒');
//存入變數
var temp = arr[9];
alert('存入變數速度測試開始')
start = Date.now();
for (let i=0;i<expTimes;i++){
test = temp;
}
end = Date.now();
timeB = (end - start)/1000;
alert('存入變數速度測試結束,耗時 ' + timeB + '秒');
//不存入變數2
alert('不存入變數速度測試2開始')
start = Date.now();
for (let i=0;i<expTimes;i++){
test = arr[9];
}
end = Date.now();
timeA2 = (end - start)/1000;
alert('不存入變數速度測試2結束,耗時 ' + timeA2 + '秒');
alert('測試完成!看結果!');
document.getElementById('demo').innerHTML = '不存入變數耗時: ' + timeA1 + '秒<br>存入變數耗時: ' + timeB + '秒<br>不存入變數二次測試耗時: ' + timeA2 + '秒';