使用Javascript動態建立表格,不同的方法,巨大的已耗用時間差異!

來源:互聯網
上載者:User
 

本來是想測試一下使用Javascript產生一個比較大的表格,大概需要多長時間,一直認為這會是一個比較固定的時間。期間用了幾種不同的方法,發現效率相差太大了。下面是測試的具體說明:

目標:產生一個2000*5的表格,每個儲存格的內容是行號+逗號+列號

方法一:使用createElement產生表格,使用insertRow和insertCell方法產生行列,儲存格的內容使用innerHTML 屬性進行填充。

方法二:使用createElement產生表格,使用CreateElement方法產生行列,儲存格的內容使用了createTextNode方法填充。

方法三:拼接表格innerHTML 屬性的字串,使用字串 += 操作符連結字串

方法四:拼接表格innerHTML 屬性的字串,各個字串追加數組裡面,最後調用數組的join方法產生目標字串。

已耗用時間比較:

方法 已耗用時間(ms)
方法一 93037
方法二 3341
方法三 2795
方法四 500

具體的程式如下:

<html>
  <head>
   <title>test page</title>
   <script type='text/javascript'>
     <!--
   function createTable() {
       var t = document.createElement('table');
       for (var i = 0; i < 2000; i++) {
        var r = t.insertRow();
        for (var j = 0; j < 5; j++) {
         var c = r.insertCell();
         c.innerHTML = i + ',' + j;
        }
       }
      
       document.getElementById('table1').appendChild(t);
      t.setAttribute('border', '1');
   }
   
   function createTable2() {
       var t = document.createElement('table');
       var b = document.createElement('tbody');
       for (var i = 0; i < 2000; i++) {
        var r = document.createElement('tr');
        for (var j = 0; j < 5; j++) {
         var c = document.createElement('td');
         var m = document.createTextNode(i + ',' + j);
         c.appendChild(m);
         r.appendChild(c);
        }
        b.appendChild(r);
       }
      
       t.appendChild(b);
       document.getElementById('table1').appendChild(t);
      t.setAttribute('border', '1');
   }
   
   function createTable3() {
    var data = '';
    
    data += '<table border=1><tbody>';
       for (var i = 0; i < 2000; i++) {
        data += '<tr>';
        for (var j = 0; j < 5; j++) {
         data += '<td>' + i + ',' + j + '</td>';
        }
        data += '</tr>';
       }
       data += '</tbody><table>';
      
       document.getElementById('table1').innerHTML = data;
   }

   function createTable4() {
    var data = new Array();
    
    data.push('<table border=1><tbody>');
       for (var i = 0; i < 2000; i++) {
        data.push('<tr>');
        for (var j = 0; j < 5; j++) {
         data.push('<td>' + i + ',' + j + '</td>');
        }
        data.push('</tr>');
       }
       data.push('</tbody><table>');
      
       document.getElementById('table1').innerHTML = data.join('');
   }

   function showFunctionRunTime(f) {
    var t1 = new Date();
    f();
    var t2 = new Date();
    alert(t2 - t1);
   }
     //-->
   </script>
  </head>
 <body>
  <div id="table1" style="border: 1px solid black">
  </div>

  <script>
   showFunctionRunTime(createTable);
   showFunctionRunTime(createTable2);
   showFunctionRunTime(createTable3);
   showFunctionRunTime(createTable4);
  </script>
 </body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.