七段

          無論怎樣,請讓我先感謝一下國家。

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            35 Posts :: 2 Stories :: 7 Comments :: 0 Trackbacks
          Efficient JavaScript coding
          1, 盡可能選擇高效的method
          e.g.
          如果沒有必要,可以不用regular expression
          String.indexOf, String.lastIndexOf > String.match, String.search, String.replace

          2, 面對large loop就要斤斤計較
          2.1 Create once, use repeatedly
          for( var i = 0, oNode; oNode = oElement.childNodes[i]; i++ ) {
          if( oNode.nodeValue.match(/^\s*extra.*free/g) ) {
          //this creates the expression
          //it will be cached and re-used next time through the loop
          }
          }
          for( var i = 0, oNode; oNode = oElement.childNodes[i]; i++ ) {
          if( oNode.nodeValue.match(new RegExp(“^\s*extra.*free”,”g”)) ) {
          //this will always create a new copy of the expression,
          //and is generally much slower than creating static expressions.
          }
          }

          2.2 Referencing element once, use repeatedly
          var _table =$("#tableId")
          for (var index in json) {
          otherFun(_table, json[index]);
          };


          3 eval is evil
          Eval 或者 new Function() 執行時,瀏覽器先創建整個scripting環境(就像一個新頁面),導入scope chain中所有變量,執行script,gc, 最后導出所有變量到當前環境。(in a word, cost much)另外,js engine還不能對它們進行cache優化。

          4 less is more
          Less code, short naming
          Only add event listener what you need

          5 do less
          Take a short circuit
          e.g
          var logger=window.console && window.console.dir
          var logger=window.console || {}

          less XHR calling
          e.g. enable cache for the same request

          6 Reduce reflow
          每當添加element到document里,browser就會reflow整個頁面去計算如何重新定位和渲染。

          7,cache
          Enable cache for duplicated XHR calling
          Enable cache for js script file, so move out jscript from jsp to js file.

          Reference:
          http://slowjavascript.com/JavaScript_Performance_Rocks_Checklist.pdf


          已有 0 人發表留言,猛擊->>這里<<-參與討論


          JavaEye推薦




          文章來源:http://sevenduan.javaeye.com/blog/505272
          posted on 2009-10-31 14:49 sevenduan 閱讀(184) 評論(0)  編輯  收藏 所屬分類: JavaScript
          主站蜘蛛池模板: 大悟县| 黄大仙区| 永顺县| 镇安县| 镇原县| 合肥市| 会昌县| 会宁县| 大同市| 抚顺县| 多伦县| 东乌珠穆沁旗| 普洱| 香港| 华宁县| 米易县| 云浮市| 荣成市| 深州市| 那坡县| 万载县| 吉木乃县| 雅安市| 隆尧县| 嘉黎县| 郎溪县| 巴塘县| 兴安盟| 社旗县| 贞丰县| 平谷区| 奉化市| 高州市| 锦屏县| 同心县| 南木林县| 巴马| 馆陶县| 讷河市| 宁城县| 黔东|