Wildbit

The Blog

Thoughts on building web apps, businesses, and virtual teams.

Twitter

21 Nov JavaScript optimization: Adding DOM elements to document ← Go back

Posted by dshteflyuk on November 21, 2006 — 8 Comments

dshteflyuk

Scenario: you’re developing rich Web application and you need to load dynamic elements using AJAX and then add them to current document. For some reason you don’t want (or can’t) use fully generated HTML, and decided to fetch items in JavaScript array.

I know two classic ways to do so: create elements using document.createElement() method and concatenate HTML strings and assign result to parentElement.innerHTML property. Of course, you can combine both ways. Let’s examine these ways in detail.

Classic way (and in ideal world the best way) is to use DOM for element manipulations:

for (var i = 1; i < = 1000; i++) {
var li = document.createElement('li');
li.appendChild(document.createTextNode('Element ' + i));
el.appendChild(li);
}

I got 1403 ms in Internet Explorer 6 and 165 ms in Firefox 1.5. Not so bad, but what about creating DOM element from HTML code?

for (var i = 1; i < = 1000; i++) {
var li = document.createElement('<li>Element ' + i + ')
el.appendChild(li);
}

It works better in Internet Explorer (1134 ms), but does not work in Firefox at all. Weird! Of course, you can add try/catch block and create elements using first approach in catch block for Firefox. But I have a better solution.

Every DOM node has the attribute innerHTML which holds all child nodes as an HTML string.

el.innerHTML = '';
for (var i = 1; i < = 1000; i++) {
el.innerHTML += '<li>Element ' + i + '';
}

Wow, 39757 ms in Internet Explorer and 41058 ms in Firefox! Cool result, right? It’s because browser tried to render list while we updating and it takes such a long time. Little optimization:

var html = '';
for (var i = 1; i < = 1000; i++) {
html += '<li>Element ' + i + '';
}
el.innerHTML = html;

Firefox shows great performance, 50 ms, but Internet Explorer still slow – 10994 ms. I found solution which works very fast in both browsers: to create array of HTML chunks, and the join them using empty string:

var html = [];
for (var i = 1; i < = 1000; i++) {
html.push('<li>Element ');
html.push(i);
html.push('');
}
el.innerHTML = html.join('');

It’s fastest approach for Internet Explorer 6 – 407 ms, and very fast in Firefox – 47 ms. Why I’m not saying fastest in case of Firefox? I added another test to make in cleaner:

var html = '';
for (var i = 1; i < = 1000; i++) {
html += '<li style="padding-left: ' + (i % 50) +
'" id="item-' + i + '">Element ' + i + ' Column ' +
(i % 50) + '';
}
el.innerHTML = html;

And second example:

var html = [];
for (var i = 1; i < = 1000; i++) {
html.push('<li style="padding-left: ');
html.push(i % 50);
html.push('" id="item-');
html.push(i);
html.push('">Element ');
html.push(i);
html.push(' Column ');
html.push(i % 50);
html.push('');
}
el.innerHTML = html.join('');

84 ms for the first example and 110 ms for the second.

Here is results in table and diagram formats.

No Method Internet Explorer 6 Firefox 1.5
1 document.createElement() 1403 166
2 document.createElement() Full 1134
3 element.innerHTML 39757 41058
4 element.innerHTML Optimized 10994 50
5 element.innerHTML Join 400 47
6 element.innerHTML Optimized Large 28934 84
7 element.innerHTML Join Large 950 110

You can view benchmark test and get your own results here.

Image001

8 Comments

IE7 is beating up FF2.0 on these tests.

kmiuc — January 4, 2007, 8:03 am

IE7 is beating up FF2.0 on these tests.

kmiuc — January 4, 2007, 8:03 am

I am very interested in your post, but is it possible to rewriute some of your code over two lines so that we can actually read it all. I had to do ViewSource on your HTML page and then port the text over to a TextEditor to actually read your code. I would love it if I could read it in context of your actual blog! :)

Martin Roberts — May 14, 2007, 9:01 pm

ahh, i see the problem. In IE 6, it shows a horizontal scroll bar, but in Firefox 2, no horizontal scroll bar appears, so I am unable to ready your lines of code…

Martin Roberts — May 14, 2007, 9:06 pm

Your links such as “You can view benchmark test and get your own results *here*” don’t work.

This is also true for your other JavaScript benchmarking post.

Martin Robwerts — May 14, 2007, 9:15 pm

Thanks Martin. We just updated our blog and it looks like this post did not transfer over well. We will update it shortly.

Chris Nagele — May 15, 2007, 12:25 pm

Chris Nagele

Thanks
I need the best (fastest) approach to fetch HTML elements, now i am using document.getElementById(). In my page it consists of more than 2000 elements, so in IE it takes more time than firefox to fetch the object. Kindly help me.
Tanks in advance

arun — July 10, 2007, 1:40 am

Hello everybody, my name is Damion, and I’m glad to join your conmunity,
and wish to assit as far as possible.

DamionKutaeff — March 22, 2008, 5:37 pm

Write a comment

* required
* required

← Go back

Get in Touch

Wildbit, LLC

Work 20 North 3rd St, 2nd Floor
Philadelphia, PA 19106 USA

Google Maps
 
 
Fax
+1 (267) 200 0835
Email
IndyHall

We work at IndyHall. Coworking is more than just space.