views:

419

answers:

3

Hello, I'm trying to optimize a sortable table I've written. The bottleneck is in the dom manipulation. I'm currently creating new table rows and inserting them every time I sort the table. I'm wondering if I might be able to speed things up by simple rearranging the rows, not recreating the nodes. For this to make a significant difference, dom node rearranging would have to be a lot snappier than node creating. Is this the case? thanks, -Morgan

A: 

You may find this page handy for some benchmarks:

http://www.quirksmode.org/dom/innerhtml.html

Salty
+1  A: 

I don't know whether creating or manipulating is faster, but I do know that it'll be faster if you manipulate the entire table when it's not on the page and then place it on all at once. Along those lines, it'll probably be slower to re-arrange the existing rows in place unless the whole table is removed from the DOM first.

This page suggests that it'd be fastest to clone the current table, manipulate it as you wish, then replace the table on the DOM.

Daniel Lew
oh. interesting. Thanks.
morgancodes
A: 

I'm drawing this table about twice as quickly now, using innerHTML, building the entire contents as a string, rather than inserting nodes one-by-by.

morgancodes
wow, actually, that technique makes it much worse in IE6.
morgancodes
Instead of string = string + newstring, use an array: var a=[]; while(loop){ a.push(newstring) } result = a.join(""); // String concatenation in IE is terrible slow. I did a test once where Opera finished in about 4 seconds, and IE still hadn't finished after 20 minutes.
some
Yeah, I came across that too, but with what I'm doing, it seems that it's still faster to add dom nodes than use innerHTML in IE6.
morgancodes