一、for迴圈的優化

 <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
window.onload=function(){
var obj=[12,34,56,67,123,23];
circulate(obj);
circulate2(obj);
}
/*
對於for迴圈,每次開始迴圈之前都會判斷迴圈條件,滿足迴圈條件才執行後邊的語句。
這個例子中,每次迴圈都會執行 arr.length 語句來獲取陣列長度,陣列越大,執行時間越長;
*/
function circulate(arr){
for(var i=0;i<arr.length;i++){
console.info("第一種for迴圈"+arr[i]);
}
}
//提前快取陣列長度
function circulate2(arr){
for(var i=0, max=arr.length; i < max; i++){
console.info("第二種for迴圈"+arr[i]);
}
}
</script>
</head>
<body>
</body>
</html>