Performance tuning – Part 1

Posted on 10 June 2014 | Development

In the realm of front-end development, lots of DOM manipulation libraries exist. The most popular library, without a doubt, is jquery. A DOM manipulation library, providing very concise shorthand commands, cross-browser compatibility and a very gradual learning curve. All this, and an extremely large active community, contributed to its high popularity.

Can’t have it all

When all this functionality is included in a generic library some sacrifices have to be made. In jquery this comes down to performance, especially when adding elements to the DOM. When performance is paramount, or when you run into a performance drop because of a large amount of DOM elements, vanilla javascript DOM manipulation can speed things up a lot. The downside to using vanilla javascript is that it’s quite verbose (though getting better).


According to Mozilla: “The DocumentFragment interface represents a minimal document object that has no parent. It is used as a light-weight version of Document to store well-formed or potentially non-well-formed fragments of XML.”

What it comes down to is that when inserting DOM elements, it is often much faster to create a DocumentFragment, add elements to it and insert the fragment into the DOM. This alone will improve performance quite considerably.

Example: Here a document fragment is created, an H1 element is appended to it and finally the document fragment is inserted into the DOM.

DOM creation libraries

Luckily there are some nice DOM creation libraries around that don’t affect performance too much. Creating DOM elements using these libraries will allow for a more modest code base. At the time of writing, crel is the fastest of the two libraries and smaller in size as well.

Check out this jsPerf for performance results in your browser…

Let’s take the following code as an example:


“Laconic offers an intuitive approach to generating DOM content in JavaScript”. Using laconic it would look like this:

“A small, simple, and fast DOM creation utility”. Using crel it would look like this: