A proposito dell'uso di myArray[myArray.length] = valore versus myArray.push(valore)… Ho scoperto che i due metodi hanno prestazioni stranamente identiche in Firefox, mentre su Chrome si conferma molto più rapido l'uso di push – impiega la metà del tempo. Ho fatto il test con un ciclo di dieci milioni di inserimenti:
codice:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>myArray[myArray.length] vs. myArray.push()</title>
<script type="text/javascript">
function printElapsedTime (fTest) {
var nStartTime = Date.now(), vReturn = fTest(), nEndTime = Date.now();
alert("Elapsed time: " + String(nEndTime - nStartTime) + " milliseconds");
return vReturn;
}
function setNewVals () {
var myArray = [];
for (var iCount = 0; iCount < 1e7; iCount++) {
myArray[myArray.length] = "Ciao!";
}
}
function pushNewVals () {
var myArray = [];
for (var iCount = 0; iCount < 1e7; iCount++) {
myArray.push("Ciao!");
}
}
</script>
</head>
<body>
[ <span style="cursor:pointer;text-decoration:underline;color:#0000ff;" onclick="printElapsedTime(setNewVals);">Utilizza myArray[myArray.length]</span> | <span style="cursor:pointer;text-decoration:underline;color:#0000ff;" onclick="printElapsedTime(pushNewVals);">Utilizza myArray.push()</span> ]</p>
</body>
</html>