Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript/dom -- how expensive is creating vs rearranging dom nodes?

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

like image 552
morgancodes Avatar asked Feb 16 '09 18:02

morgancodes


2 Answers

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.

like image 139
Dan Lew Avatar answered Nov 16 '22 04:11

Dan Lew


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.

like image 30
morgancodes Avatar answered Nov 16 '22 02:11

morgancodes