Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to add/remove multiple elements in DOM

Tags:

javascript

dom

Edit: It's not a duplicate of "Fastest DOM insertion" because it includes the event listeners variable too. Anyway I will edit the question.

I would to know some things if someone already discovered.

I would like to know which is the fastest way to add an element in javascript and to be compatible with ie6+, chrome10+, firefox2+.

Fastest technique to:

  1. Add a single element
  2. Add 100 elements
  3. Any time curve for adding 1 element or 1000 elements

  4. The same as above but for removing an element

  5. Add/Change/Remove an inline style attribute

  6. Add event listeners like mousedown/mouseup/mouseover

  7. Is faster to add/remove some tags over others? ex: is faster to add/remove a <div> or a <p>? (In the adding mode: I think is faster <p> because it only has 1 letter vs 3 letters, and maybe because div is a container. I don't know, that's why I'm asking :D)

    1. Remove an element, save into js somehow, then re-add it and keep the event listeners working as it were before removing the element

This a big question, so if you only know some of the questions you can always share what you know ;)

thanks thanks!!

like image 651
Totty.js Avatar asked Sep 05 '11 11:09

Totty.js


1 Answers

1   Set the value of innerHTML on the parent element to a string containing HTML for the new elements.
2   Same as 1.
4   Set the value of innerHTML on the parent element to an empty string.
7.1   Call Node.removeChild(), save the returned Node object, and then pass the node to Node.appendChild().

like image 169
clarkb86 Avatar answered Oct 20 '22 18:10

clarkb86