Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest DOM insertion

Tags:

javascript

dom

What are the best practices for doing DOM insertion?

  • Is it faster to insert large chunks of html vs element at a time in a loop?
  • Does it matter what html you are inserting, or only how big the chunk is?
  • It it faster to insert a table, vs inserting just the rows using the table hack?
like image 685
mkoryak Avatar asked Mar 11 '09 14:03

mkoryak


People also ask

What is DOM insertion?

Category: DOM Insertion, InsideThese methods allow us to insert new content inside an existing element.

What is innerHTML?

The innerHTML property returns: The text content of the element, including all spacing and inner HTML tags. The innerText property returns: Just the text content of the element and all its children, without CSS hidden text spacing and tags, except <script> and <style> elements.

What is DOM manipulation technique?

As a web developer, you frequently need to manipulate the DOM , the object model that is used by browsers to specify the logical structure of web pages, and based on this structure to render HTML elements on the screen. HTML defines the default DOM structure.


1 Answers

Setting innerHTML is often faster than inserting seperate nodes.

Another possibility would be to create a DocumentFragment, which allows to insert the nodes all at once. Another advantage of using DocumentFragments is that they can easily be cloned, which can replace a lot of use cases for innerHTML and is potentially faster as no parsing is involved.

like image 124
Christoph Avatar answered Sep 28 '22 10:09

Christoph