我們先看一下三種算法以及在各種浏覽器下的表現
第一種
<!DOCTYPE HTML PUBLIC "
"
<html xmlns="
<head>
<meta http
<title>New Web Project</title>
</head>
<body>
<script>
microtime = function(get_as_float) {
var now = new Date()
var s = parseInt(now
return (get_as_float) ? now : (Math
}
var m
var table = document
table
var tbody = document
for(var i =
var tr = document
for(var j =
var td = document
td
tr
}
tbody
}
table
//chrome
//ie
//ie
//ie
//ie
//firefox
//opera
//safari
document
var m
alert(m
</script>
</body>
</html>
第二種
<!DOCTYPE HTML PUBLIC "
"
<html xmlns="
<head>
<meta http
<title>New Web Project</title>
</head>
<body>
<script>
microtime = function(get_as_float) {
var now = new Date()
var s = parseInt(now
return (get_as_float) ? now : (Math
}
var m
var table = document
table
var tbody = document
var fragment = document
for(var i =
var tr = document
for(var j =
var td = document
td
tr
}
fragment
}
tbody
table
//chrome
//ie
//ie
//ie
//ie
//firefox
//opera
//safari
document
var m
alert(m
</script>
</body>
</html>
第三種
<!DOCTYPE HTML PUBLIC "
"
<html xmlns="
<head>
<meta http
<title>New Web Project</title>
</head>
<body>
<script>
microtime = function(get_as_float) {
var now = new Date()
var s = parseInt(now
return (get_as_float) ? now : (Math
}
var m
var table = document
table
var tbody = document
table
for(var i =
tbody
for(var j =
tbody
tbody
}
var tr = document
tbody
}
//chrome
//ie
//ie
//ie
//ie
//firefox
//opera
//safari
document
var m
alert(m
</script>
</body>
</html>
以上可以看出用原生的js創建表格效率最好
對第一種算法簡單的優化了一下
<!DOCTYPE HTML PUBLIC "
"
<html xmlns="
<head>
<meta http
<title>New Web Project</title>
</head>
<body>
<script>
microtime = function(get_as_float) {
var now = new Date()
var s = parseInt(now
return (get_as_float) ? now : (Math
}
var m
var table = document
table
var tbody = document
var i =
while(i
var tr = document
while(j
var td = document
td
tr
}
tbody
}
table
//chrome
//ie
//ie
//ie
//ie
//firefox
//opera
//safari
document
var m
alert(m
</script>
</body>
</html>
總結
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19826.html