JSFiddle - React, Tailwind, and code Playground
JavaScript
var rooms = {};
rooms['test'] = {
players: {},
drawer: {},
answer: '',
msgHistory: [],
drawingHistory: [],
msgNum: 0,
pixelNum: 0,
settings: {
maxPlayers: 12,
gameMode: 'drawingAndGuess'
}
};
var room = rooms['test'],
msgHistory = room.msgHistory,
msgNum = room.msgNum,
drawingHistory = room.drawingHistory,
pixelNum = room.pixelNum;
var expTimes = 10000000;
//不存入變數1
alert('不存入變數速度測試1開始')
start = Date.now();
for (let i=0;i<expTimes;i++){
rooms['test'].drawingHistory[rooms['test'].pixelNum-1] = i;
}
end = Date.now();
timeA1 = (end - start)/1000;
alert('不存入變數速度測試1結束,耗時 ' + timeA1 + '秒');
//存入變數
alert('存入變數速度測試開始')
start = Date.now();
for (let i=0;i<expTimes;i++){
drawingHistory[pixelNum-1] = i;
}
end = Date.now();
timeB = (end - start)/1000;
alert('存入變數速度測試結束,耗時 ' + timeB + '秒');
//不存入變數2
alert('不存入變數速度測試2開始')
start = Date.now();
for (let i=0;i<expTimes;i++){
rooms['test'].drawingHistory[rooms['test'].pixelNum-1] = i;
}
end = Date.now();
timeA2 = (end - start)/1000;
alert('不存入變數速度測試2結束,耗時 ' + timeA2 + '秒');
alert('測試完成!看結果!');
document.getElementById('demo').innerHTML = '不存入變數耗時: ' + timeA1 + '秒<br>存入變數耗時: ' + timeB + '秒<br>不存入變數二次測試耗時: ' + timeA2 + '秒';