Some interesting food for thought here. In my experience, several things hold true when running scripts:
1. Firefox is always slower than IE
2. Firefox is always more inconsistent in script timings over multiple runs than IE
3. DOM methods are always slower than using innerHTML
So, with that in mind, I found it interesting that DOM methods have been found to be quicker. With this thread in mind, I came up with the following script:
Code:
<html>
<head>
<script type="text/javascript">
<!--
function createInputsUsingDOM(numberToCreate) {
var frm = document.getElementById('myForm');
for (var loop=0; loop<numberToCreate; loop++) {
var newElement = document.createElement('input');
newElement.type = 'text';
newElement.name = 'name-' + (loop+1);
frm.appendChild(newElement);
}
}
function createInputsUsingInnerHTML(numberToCreate) {
var frm = document.getElementById('myForm');
var s = '';
for (var loop=0; loop<numberToCreate; loop++) {
s += '<input type="text" name="name-' + (loop+1) + '">';
}
frm.innerHTML = s;
}
onload = function() {
var timeStart = new Date();
//createInputsUsingDOM(100);
//createInputsUsingInnerHTML(100);
var timeEnd = new Date();
alert('Time taken: ' + (timeEnd.getTime() - timeStart.getTime()));
}
//-->
</script>
</head>
<body>
<form id="myForm">
</form>
</body>
</html>
The relevant method is uncommented, then the number of iterations set. Then, the page was loaded, and refreshed after noting down each time.
I didn't know what the average number of form elements to be created was, so I tried several tests - with 100 elements, and with 10,000 elements. I ran each test 5 times, in both Firefox and IE. I found my previous 3 observations to hold true. All timings are in msecs:
100 elements, Firefox (DOM Method)
140, 109, 141, 109, 125
100 elements, IE (DOM Method)
16, 16, 0, 0, 15
10,000 elements, Firefox (innerHTML Method)
109, 93, 93, 94, 93
10,000 elements, IE (innerHTML Method)
0, 0, 0, 16, 0
However... something bugged me. The fact that kaht's colleague managed to get DOM methods to be significantly faster than innerHTML, when I've always experienced it the other way around (on many PCs with varying clock speeds).
So... I added another function:
Code:
function createInputsUsingInnerHTML2(numberToCreate) {
var frm = document.getElementById('myForm');
for (var loop=0; loop<numberToCreate; loop++) {
frm.innerHTML += '<input type="text" name="name-' + (loop+1) + '">';
}
}
This one appended to the innerHTML of the form, rather than setting it in one go. The time? Well... let's just say that I managed to leave it running, go and watch an entire episode of Stargate SG1, and start watching another episode before it returned. The final time: 3420375 msecs, or 57 minutes.
I'm not saying that innerHTML will always be slower... nor am I saying that my tests are conclusive for all systems or all browsers... but they certainly re-inforce my prior observations, and those I suspected I would find.
Hope this helps,
Dan
[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]