javascript

位置:IT落伍者 >> javascript >> 浏览文章

解析js原生方法创建表格效率测试


发布日期:2020年06月22日
 
解析js原生方法创建表格效率测试
对于创建表格本文提到的算法共有分别是直接操作dom借助createDocumentFragment借助js的原生表格操作方法

我们先看一下三种算法以及在各种浏览器下的表现
第一种 直接操作dom

复制代码 代码如下:
<!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN"
"
<html xmlns="
<head>
<meta httpequiv="ContentType" content="text/html; charset=utf" />
<title>New Web Project</title>
</head>
<body>
<script>
microtime = function(get_as_float) {
var now = new Date()getTime() / ;
var s = parseInt(now );
return (get_as_float) ? now : (Mathround((now s) * ) / ) + + s;
}
var m = microtime(true);
var table = documentcreateElement("table");
tableborder = ;
var tbody = documentcreateElement("tbody");
for(var i = ; i < ; i++ ) {
var tr = documentcreateElement("tr");
for(var j = ; j < ; j++ ) {
var td = documentcreateElement("td");
tdappendChild(documentcreateTextNode("cell "+i+""+j));
trappendChild(td);
}
tbodyappendChild(tr);
}
tableappendChild(tbody);
//chrome
//ie
//ie
//ie
//ie
//firefox
//opera
//safari
documentbodyappendChild(table);
var m = microtime(true);
alert(mm);
</script>
</body>
</html>


第二种借助借助createDocumentFragment

复制代码 代码如下:
<!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN"
"
<html xmlns="
<head>
<meta httpequiv="ContentType" content="text/html; charset=utf" />
<title>New Web Project</title>
</head>
<body>
<script>
microtime = function(get_as_float) {
var now = new Date()getTime() / ;
var s = parseInt(now );
return (get_as_float) ? now : (Mathround((now s) * ) / ) + + s;
}
var m = microtime(true);
var table = documentcreateElement("table");
tableborder = ;
var tbody = documentcreateElement("tbody");
var fragment = documentcreateDocumentFragment();
for(var i = ; i < ; i++ ) {
var tr = documentcreateElement("tr");
for(var j = ; j < ; j++ ) {
var td = documentcreateElement("td");
tdappendChild(documentcreateTextNode("cell "+i+""+j));
trappendChild(td);
}
fragmentappendChild(tr);
}
tbodyappendChild(fragment);
tableappendChild(tbody);
//chrome
//ie
//ie
//ie
//ie
//firefox
//opera
//safari
documentbodyappendChild(table);
var m = microtime(true);
alert(mm);
</script>
</body>
</html>


第三种借助js的原生表格操作方法

复制代码 代码如下:
<!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN"
"
<html xmlns="
<head>
<meta httpequiv="ContentType" content="text/html; charset=utf" />
<title>New Web Project</title>
</head>
<body>
<script>
microtime = function(get_as_float) {
var now = new Date()getTime() / ;
var s = parseInt(now );
return (get_as_float) ? now : (Mathround((now s) * ) / ) + + s;
}
var m = microtime(true);
var table = documentcreateElement("table");
tableborder = ;
var tbody = documentcreateElement("tbody");
tableappendChild(tbody);
for(var i = ; i < ; i++ ) {
tbodyinsertRow(i);
for(var j = ; j < ; j++ ) {
tbodyrows[i]insertCell(j);
tbodyrows[i]cells[j]appendChild(documentcreateTextNode("cell "+i+""+j));
}
var tr = documentcreateElement("tr");
tbodyappendChild(tr);
}
//chrome
//ie
//ie
//ie
//ie
//firefox
//opera
//safari
documentbodyappendChild(table);
var m = microtime(true);
alert(mm);
</script>
</body>
</html>


以上可以看出用原生的js创建表格效率最好借助createDocumentFragment优势不是很大(并不像网上说的那么明显)借助insertRow和insertCell等在ie下效率太低不建议使用

对第一种算法简单的优化了一下

复制代码 代码如下:
<!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN"
"
<html xmlns="
<head>
<meta httpequiv="ContentType" content="text/html; charset=utf" />
<title>New Web Project</title>
</head>
<body>
<script>
microtime = function(get_as_float) {
var now = new Date()getTime() / ;
var s = parseInt(now );
return (get_as_float) ? now : (Mathround((now s) * ) / ) + + s;
}
var m = microtime(true);
var table = documentcreateElement("table");
tableborder = ;
var tbody = documentcreateElement("tbody");
var i = ;
while(i){
var tr = documentcreateElement("tr") j = ;
while(j){
var td = documentcreateElement("td");
tdappendChild(documentcreateTextNode("cell "+i+""+j));
trappendChild(td);
}
tbodyappendChild(tr);
}
tableappendChild(tbody);
//chrome
//ie
//ie
//ie
//ie
//firefox
//opera
//safari
documentbodyappendChild(table);
var m = microtime(true);
alert(mm);
</script>
</body>
</html>


总结对dom操作尽量使用createElement和appendChild对于js内核提供的一些特殊方法慎用

               

上一篇:中文路径导致unitpngfix.js不正常的解决方法

下一篇:js 控制网页打印大全