Tutorial :jquery: fastest DOM insertion?


I got this bad feeling about how I insert larger amounts of HTML. Lets assume we got:

var html="<table>..<a-lot-of-other-tags />..</table>"

and I want to put this into


previously I did something like

var html_obj = $(html); $("#mydiv").append(html_obj);

Is it correct that jQuery is parsing html to create DOM-Objects ? Well this is what I read somewhere (UPDATE: I meant that I have read, jQuery parses the html to create the whole DOM tree by hand - its nonsense right?!), so I changed my code:

$("#mydiv").attr("innerHTML", $("#mydiv").attr("innerHTML") + html);

Feels faster, is it ? And is it correct that this is equivalent to:

document.getElementById("mydiv").innerHTML += html ? or is jquery doing some additional expensive stuff in the background ?

Would love to learn alternatives as well.


innerHTML is remarkably fast, and in many cases you will get the best results just setting that (I would just use append).

However, if there is much already in "mydiv" then you are forcing the browser to parse and render all of that content again (everything that was there before, plus all of your new content). You can avoid this by appending a document fragment onto "mydiv" instead:

var frag = document.createDocumentFragment();  frag.innerHTML = html;  $("#mydiv").append(frag);  

In this way, only your new content gets parsed (unavoidable) and the existing content does not.

EDIT: My bad... I've discovered that innerHTML isn't well supported on document fragments. You can use the same technique with any node type. For your example, you could create the root table node and insert the innerHTML into that:

var frag = document.createElement('table');  frag.innerHTML = tableInnerHtml;  $("#mydiv").append(frag);  


Try the following:


The other answers, including the accepted answer, are slower by 2-10x: jsperf.

The accepted answer does not work in IE 6, 7, and 8 because you can't set innerHTML of a <table> element, due to a bug in IE: jsbin.


What are you attempting to avoid? "A bad feeling" is incredibly vague. If you have heard "the DOM is slow" and decided to "avoid the DOM", then this is impossible. Every method of inserting code into a page, including innerHTML, will result in DOM objects being created. The DOM is the representation of the document in your browser's memory. You want DOM objects to be created.

The reason why people say "the DOM is slow" is because creating elements with document.createElement(), which is the official DOM interface for creating elements, is slower than using the non-standard innerHTML property in some browsers. This doesn't mean that creating DOM objects is bad, it is necessary to create DOM objects, otherwise your code wouldn't do anything at all.


The answer about using a DOM fragment is on the right track. If you have a bunch of html objects that you are constant inserting into the DOM then you will see some speed improvements using the fragment. This post by John Resig explains it pretty well: http://ejohn.org/blog/dom-documentfragments/


For starters, write a script that times how long it takes to do it 100 or 1,000 times with each method.

To make sure the repeats aren't somehow optimized away--I'm no expert on JavaScript engines--vary the html you're inserting every time, say by putting '0001' then '0002' then '0003' in a certain cell of the table.


The fastest way to append items

The fastest way to append to the DOM tree is to buffer all of your append in to a single DOM fragment, then append the dom fragment to the dom.

This is the method I use in my game engine.

//Returns a new Buffer object  function Buffer() {        //the framgment      var domFragment = document.createDocumentFragment();        //Adds a node to the dom fragment      function add(node) {          domFragment.appendChild(node);      }        //Flushes the buffer to a node      function flush(targetNode) {            //if the target node is not given then use the body          var targetNode = targetNode || document.body;            //append the domFragment to the target          targetNode.appendChild(domFragment);        }        //return the buffer      return {          "add": add,          "flush": flush      }  }      //to make a buffer do this  var buffer = Buffer();    //to add elements to the buffer do the following  buffer.add(someNode1);    //continue to add elements to the buffer  buffer.add(someNode2);  buffer.add(someNode3);  buffer.add(someNode4);  buffer.add(someN...);    //when you are done adding nodes flush the nodes to the containing div in the dom  buffer.flush(myContainerNode);  

Using this object i am able to render ~1000 items to the screen ~40 times a second in firefox 4.

Here's a use case.


I create a giant string with and then append this string with jquery. Works good and fast, for me.


You mention being interested in alternatives. If you look at the listing of DOM-related jQuery plugins you'll find several that are dedicated to programatically generating DOM trees. See for instance SuperFlyDom or DOM Elements Creator; but there are others.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »