<div id="writeroot" style="width:1px; height:1px; overflow:hidden;"></div>
<script>
function removeTable() {
document.getElementById('writeroot').innerHTML = '';
}
</script>
W3CDOM 1: Create all elements as needed:-
removeTable();
var x = document.createElement('table');
var y = x.appendChild(document.createElement('tbody'));
for (var i = 0; i < 20; i++) {
var z = y.appendChild(document.createElement('tr'));
for (var j = 0; j < 20; j++) {
var a = z.appendChild(document.createElement('td'));
a.appendChild(document.createTextNode('*'));
}
}
document.getElementById('writeroot').appendChild(x);
Result is 55 % slower as compare others.
W3CDOM 2: Create elements once, then clone:-
removeTable();
var x = document.createElement('table');
var y = x.appendChild(document.createElement('tbody'));
var tr = document.createElement('tr');
var td = document.createElement('td');
var ast = document.createTextNode('*');
for (var i = 0; i < 20; i++) {
var z = y.appendChild(tr.cloneNode(true));
for (var j = 0; j < 20; j++) {
var a = z.appendChild(td.cloneNode(true));
a.appendChild(ast.cloneNode(true));
}
}
document.getElementById('writeroot').appendChild(x);
Result is 36 % slower as compare others.
removeTable();
var x = document.createElement('table');
var y = x.appendChild(document.createElement('tbody'));
for (var i = 0; i < 20; i++) {
var z = y.insertRow(0);
for (var j = 0; j < 20; j++) {
var a = z.insertCell(0).appendChild(document.createTextNode('*'));
}
}
document.getElementById('writeroot').appendChild(x);
Result is 50 % slower as compare others.
INNERHTML 1: concatenate one string:-
removeTable();
var string = '<table><tbody>';
for (var i = 0; i < 20; i++) {
string += '<tr>';
for (var j = 0; j < 20; j++) {
string += '<td>*</td>';
}
string += '</tr>';
}
string += '</tbody></table>';
document.getElementById('writeroot').innerHTML = string;
Result is 5 % fastest as compare others.
INNERHTML 2: push and join:-
removeTable();
var string = new Array();
string.push('<table><tbody>');
for (var i = 0; i < 20; i++) {
string.push('<tr>');
for (var j = 0; j < 20; j++) {
string.push('<td>*</td>');
}
string.push('</tr>');
}
string.push('</tbody></table>');
var writestring = string.join('');
document.getElementById('writeroot').innerHTML = writestring;
Result is 2% slower than others tests.
Actual results are as follows.
Columns as as follows. innerHTML1,innerHTML2,W3CDOM 1,W3CDOM 2,tableMethods and No of Tests
Chrome 8.0.552 197 194 617 647 634 10
Chrome 9.0.597 175 180 349 362 398 5
Chrome 10.0.612 202 207 743 718 684 3
Firefox 3.6.11 93 90 81 71 79 1
Firefox 3.6.12 208 204 177 150 172 4
Firefox 3.6.13 115 112 105 86 106 3
Firefox Beta
4.0b7 786 696 508 409 378 7
IE 6.0 20 84 18 19 10 8
IE 8.0 240 234 43 47 47 9
iPhone 4.2.1 18 19 47 49 49 1
Opera 11.00 772 752 347 491 383 1
Safari 5.0.2 190 196 616 607 589 1
Safari 5.0.3 209 219 623 595 584 11
So Finally inner HTML won and it's fastest among all methods.
No comments:
Post a Comment