Dom Manipulation - Js Templating vs programmatic jQuery

JavaScript performance comparison

Test case created by Thomas Davis


What is faster, generating elements on the fly using jQuery appends and generator or using javascript templates.

Preparation code

<script src=""></script>
<script src=""></script>

<!-- The Javascript template - using underscore.js -->
<script type="text/template" id="combo_template">
    <% _.each(values, function(value) { %>
        <option><%= value %></option>
    <% }); %>
    </select >

<div id="testcontainer" style="display: none;"></div>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Javascript Templates
var html = $("#combo_template").html();
$("#testcontainer").append(_.template(html, {
  values: values
jQuery Append
var combo = $("<select/>");
var blah = values.length;
for( var i = 0; i<blah;i++){
  var value = $("<option/>");
Stringed Dom
var HTML = '<select>', blah = values.length;
for( var i = 0; i<blah;i++){
HTML += '<option>' + values[i] + '</option>';
HTML += '</select>';

// Fast but too messy imo

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:


Thomas Davis (revision owner) commented :

lol at IE speeds

Stuart commented :

I just added IE6 (for giggles), Opera, some iPads, iPhones, Camino and the latest Chrome Canary build. In other news... if you need advice on how to party it up on a Friday night just ask away.

Thomas Davis (revision owner) commented :

I need friday night party ideas for a house full of dudes and no chance of women.

Stuart commented :

May I humbly suggest you get everyone a suite of outdated browsers and have them start posting test results on this particular Dom Manipulation? Slowest one wins. Then get everyone a bunch of the latest nightlies, fastest one wins.

Or just go where the women are. I find that does wonders for one's chances.

Thomas Davis (revision owner) commented :

Checkout the revisions for some other cool methods, I really like revision 6

Shawn Crigger commented :

This is a very interesting test suite, I've been researching using Java script templates this week and seeing that they actually work a bit faster then using jQuery's Append is a plus.

Comment form temporarily disabled.

Add a comment