I have a search that returns JSON, which I then transform into a HTML table in Javascript. It repeatedly calls the jQuery.append() method, once for each row. I have a modern m
I've done this before. It's all the DOM manipulation that slows things down because of the repaint/reflow process firing after each addition.
Build it as a string on the client, insert the string into the DOM using .html()
.
This has worked quite successfully for me, even on IE6.
As others have mentioned, excessive DOM manipulation kills performance. Creating an HTML string using the aforementioned Array.join('') and setting the innerHTML of a container using the jQuery.html() method is orders of magnitude faster. Be wary of using jQuery.append(html) - this is equivalent to creating all the DOM nodes first and then inserting them!
Thing is, even if you optimize the creation of the page node tree, you're still going to hit a ceiling pretty fast with very large datasets. Browsers just can't handle such large and complex DOM trees. First thing you will see slowing down will be the interactions (animations, handlers, etc.) even if you use event delegation. If your dataset is truly large, you will need to do some sort of virtualization to only show what is visible in the viewport (this is what SlickGrid does - http://github.com/mleibman/slickgrid).
Alternatively, you can improve the responsiveness and "time to interactive" of your page by chunking your DOM additions and executing them on a timeout one after another with some pause in between to let the browser handle user events.
Other techniques include rendering the first page worth of data, allocating room for more, but only rendering it when the user starts scrolling towards it. This is what Facebook does.
I highly reccomend the jQuery templating plugin,
I have been using John Resig's micro-templating function for a few months. The templating plugin is the evolution of that. I have been writing and presenting on it all year :)
My Blog
Multiple DOM append operations will kill performance. And you may run into a problem with string immutability as well.
Keep the data as small as possible (JSON arrays are good), and build the html in script avoiding the javascript string concatenation problem. Append the html values to an array, and then join the array afterwards. Do one DOM append once the html has been created. eg
var builder = [];
//Inside a loop
builder.push('<tr><td>');
builder.push(json.value);
builder.push('</tr>');
//Outside the loop
$('div').append(builder.join(''));