I have a table with a lot of rows (like 10.000).
(Note: I realize it doesn't make sense to have a table that large, but I'd like to understand the below behavior).
When I receive new data I want to clear the rows first. Weirdly enough, clearing the table takes much longer than building the rows from scratch. Clearing the table rows with html("")
or plain JS innerHTML = ""
takes 1.5 minutes, much more than building the rows themselves, which takes less than a second.
So my question:
- Why does removing elements take more time than adding them? What happens 'behind the scenes'?
(Please note my question is a Why one, I'm not looking for possible workarounds).
UPDATE:
I noticed that the table row and cells have floating definitions applied to them, and when I remove the float, the table is emptied out in an instance.
I'd still really like to understand why the float makes the removal of the rows so much slower.
Here a fiddle: https://jsfiddle.net/maaikeb/t5pduuue/
In Chrome it takes 25 seconds to empty the table, and only 23 ms to append it to the DOM.
*I read the below posts, but they talk more about possible solutions, and less about why removing elements takes more time than adding them, what actually happens when you remove them
jQuery empty is very slow
jQuery empty is very slow
What is the best way to remove a table row with jQuery?
Deleting table rows in javascript
jquery - fastest way to remove all rows from a very large table