Posted in Javascript onJuly 08, 2013
我们先看一下三种算法以及在各种浏览器下的表现。
第一种: 直接操作dom。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>New Web Project</title> </head> <body> <script> microtime = function(get_as_float) { var now = new Date().getTime() / 1000; var s = parseInt(now, 10); return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s; } var m1 = microtime(true); var table = document.createElement("table"); table.border = 1; var tbody = document.createElement("tbody"); for(var i = 0; i < 1000; i++ ) { var tr = document.createElement("tr"); for(var j = 0; j < 5; j++ ) { var td = document.createElement("td"); td.appendChild(document.createTextNode("cell "+i+","+j)); tr.appendChild(td); } tbody.appendChild(tr); } table.appendChild(tbody); //chrome 0.028 //ie6 0.65 //ie7 0.40 //ie8 0.40 //ie9 0.35 //firefox14 0.035 //opera12 0.03 //safari5.17 0.014 document.body.appendChild(table); var m2 = microtime(true); alert(m2-m1); </script> </body> </html>
第二种,借助借助createDocumentFragment。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>New Web Project</title> </head> <body> <script> microtime = function(get_as_float) { var now = new Date().getTime() / 1000; var s = parseInt(now, 10); return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s; } var m1 = microtime(true); var table = document.createElement("table"); table.border = 1; var tbody = document.createElement("tbody"); var fragment = document.createDocumentFragment(); for(var i = 0; i < 1000; i++ ) { var tr = document.createElement("tr"); for(var j = 0; j < 5; j++ ) { var td = document.createElement("td"); td.appendChild(document.createTextNode("cell "+i+","+j)); tr.appendChild(td); } fragment.appendChild(tr); } tbody.appendChild(fragment); table.appendChild(tbody); //chrome 0.03 //ie6 0.68 //ie7 0.43 //ie8 0.43 //ie9 0.37 //firefox14 0.03 //opera12 0.04 //safari5.17 0.023 document.body.appendChild(table); var m2 = microtime(true); alert(m2-m1); </script> </body> </html>
第三种:借助js的原生表格操作方法
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>New Web Project</title> </head> <body> <script> microtime = function(get_as_float) { var now = new Date().getTime() / 1000; var s = parseInt(now, 10); return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s; } var m1 = microtime(true); var table = document.createElement("table"); table.border = 1; var tbody = document.createElement("tbody"); table.appendChild(tbody); for(var i = 0; i < 1000; i++ ) { tbody.insertRow(i); for(var j = 0; j < 5; j++ ) { tbody.rows[i].insertCell(j); tbody.rows[i].cells[j].appendChild(document.createTextNode("cell "+i+","+j)); } var tr = document.createElement("tr"); tbody.appendChild(tr); } //chrome 0.19 //ie9 0.18 //ie8 0.25 //ie7 8.50 //ie6 20.45 //firefox14 0.065 //opera12 0.25 //safari5.17 0.18 document.body.appendChild(table); var m2 = microtime(true); alert(m2-m1); </script> </body> </html>
以上可以看出用原生的js创建表格效率最好,借助createDocumentFragment优势不是很大(并不像网上说的那么明显),借助insertRow和insertCell等在ie6、7下效率太低,不建议使用。
对第一种算法简单的优化了一下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>New Web Project</title> </head> <body> <script> microtime = function(get_as_float) { var now = new Date().getTime() / 1000; var s = parseInt(now, 10); return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s; } var m1 = microtime(true); var table = document.createElement("table"); table.border = 1; var tbody = document.createElement("tbody"); var i = 1000; while(i--){ var tr = document.createElement("tr"), j = 5; while(j--){ var td = document.createElement("td"); td.appendChild(document.createTextNode("cell "+i+","+j)); tr.appendChild(td); } tbody.appendChild(tr); } table.appendChild(tbody); //chrome 0.03 //ie6 0.66 //ie7 0.41 //ie8 0.41 //ie9 0.35 //firefox14 0.03 //opera12 0.03 //safari5.17 0.013 document.body.appendChild(table); var m2 = microtime(true); alert(m2-m1); </script> </body> </html>
总结:对dom操作尽量使用createElement和appendChild,对于js内核提供的一些特殊方法慎用。
解析js原生方法创建表格效率测试
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@