js 优化

一、for循环的优化

 1 <!doctype html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6     <script type="text/javascript">
 7     window.onload=function(){
 8         var obj=[12,34,56,67,123,23];
 9         circulate(obj);
10         circulate2(obj);
11     }
12     /*
13     对于for循环,每次开始循环之前都会判断循环条件,满足循环条件才执行后边的语句。
14     这个例子中,每次循环都会执行 arr.length 语句来获取数组长度,数组越大,执行时间越长;
15     */
16     function circulate(arr){
17         for(var i=0;i<arr.length;i++){
18             console.info("第一种for循环"+arr[i]);
19         }
20     }
21     //提前缓存数组长度
22     function circulate2(arr){
23         for(var i=0, max=arr.length; i < max; i++){
24             console.info("第二种for循环"+arr[i]);
25         }
26     }
27     </script>
28 </head>
29 <body>
30 </body>
31 </html>
View Code
每天用心记录一点点。内容也许不重要,但习惯很重要!
原文地址:https://www.cnblogs.com/jalja/p/5138545.html