performance comparison of jQuery Template vs underscore vs string append vs mustache vs handlebars vs icanhaz vs milk vs swig for table (v74)

Revision 74 of this benchmark created by atma on


Description

To test performance of jQuery template for outputting 100 rows each with 10 columns where the columns have fields that can trigger events.

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
</script>
<script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.js">
</script>
<script src="http://documentcloud.github.com/underscore/underscore-min.js">
</script>
<script src="http://github.com/janl/mustache.js/raw/master/mustache.js">
</script>
<script src="https://raw.github.com/BorisMoore/jsrender/master/jsrender.js">
</script>
<script src="https://github.com/andyet/ICanHaz.js/raw/master/ICanHaz.min.js">
</script>
<script src="http://github.com/pvande/Milk/raw/master/dist/v1.2.0/milk.js">
</script>
<script src="http://dl.dropbox.com/u/426176/js/swig/swig.js">
</script>
<script src="http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.0.beta.6.js" type="text/javascript"></script>
<script>
  $(function() {
    $('body').delegate('#table .edit a, #table .select a', 'click', function() {
      var $td = $(this).closest('td');
      $td.find('input, select').show();
      $td.find('a').hide();
    });

    $('body').delegate('#table .edit .cancel, $table .select .cancel', 'click', function() {
      var $td = $(this).closest('td');
      $td.find('input, select').hide();
      $td.find('a').show();
    });
  });
</script>
<script type="text/x-jquery-tmpl" id="tmplRow">
  < tr > < td > < input type = "checkbox"
  value = "${id}" / > < /td>
  <td>${firstName} ${lastName}</td > < td class = "edit" > < a > Edit < /a>
    <input style="display:none;" type="hidden" value="Blah" / > < input class = "cancel"
  type = "button"
  value = "cancel" / > < /td>
  <td class="select">
      <a>Select</a > < select style = "display:none;" > < option > 0 < /option>
        <option>1</option > < option > 2 < /option>
        <option>3</option > < option > 4 < /option>
        <option>5</option > < option > 6 < /option>
        <option>7</option > < option > 8 < /option>
        <option>9</option > < option > 10 < /option>
       </select > < input class = "cancel"
  type = "button"
  value = "cancel" / > < /td>
   <td>More string</td > < td > More string < /td>
   <td>More string</td > < td > More string < /td>
   <td>More string</td > < td > More string < /td>
</tr >
</script>

<script type="text/x-handlebars-template" id="handlebarsTmpl">
{{#each rows}}
{{#with this}}
< tr > < td > < input type = "checkbox"
  value = "{{id}}" / > < /td>
  <td>{{firstName}} {{lastName}}</td > < td class = "edit" > < a > Edit < /a>
    <input style="display:none;" type="hidden" value="Blah" / > < input class = "cancel"
  type = "button"
  value = "cancel" / > < /td>
  <td class="select">
      <a>Select</a > < select style = "display:none;" > < option > 0 < /option>
        <option>1</option > < option > 2 < /option>
        <option>3</option > < option > 4 < /option>
        <option>5</option > < option > 6 < /option>
        <option>7</option > < option > 8 < /option>
        <option>9</option > < option > 10 < /option>
       </select > < input class = "cancel"
  type = "button"
  value = "cancel" / > < /td>
   <td>More string</td > < td > More string < /td>
   <td>More string</td > < td > More string < /td>
   <td>More string</td > < td > More string < /td>
</tr >
{{/with}}
{{/each}}
</script>

<script type="text/html" id="icanhazTmpl">
{{#rows}}
< tr > < td > < input type = "checkbox"
  value = "{{id}}" / > < /td>
  <td>{{firstName}} {{lastName}}</td > < td class = "edit" > < a > Edit < /a>
    <input style="display:none;" type="hidden" value="Blah" / > < input class = "cancel"
  type = "button"
  value = "cancel" / > < /td>
  <td class="select">
      <a>Select</a > < select style = "display:none;" > < option > 0 < /option>
        <option>1</option > < option > 2 < /option>
        <option>3</option > < option > 4 < /option>
        <option>5</option > < option > 6 < /option>
        <option>7</option > < option > 8 < /option>
        <option>9</option > < option > 10 < /option>
       </select > < input class = "cancel"
  type = "button"
  value = "cancel" / > < /td>
   <td>More string</td > < td > More string < /td>
   <td>More string</td > < td > More string < /td>
   <td>More string</td > < td > More string < /td>
</tr >
{{/rows}}
</script>

<script type="text/template" id="swigTmpl">
{% for row in rows %}
<tr>
  <td><input type="checkbox" value="{{ row.id }}" />< /td>
  <td>{{ row.firstName }} {{ row.lastName }}</td>
  <td class="edit">
    <a>Edit</a>
    <input style="display:none;" type="hidden" value="Blah" />
    <input class="cancel" type="button" value="cancel" />
  </td>
  <td class="select">
    <a>Select</a >
    <select style="display:none;">
      <option>0</option>
      <option>1</option><option>2</option>
      <option>3</option><option>4</option>
      <option>5</option><option>6</option>
      <option>7</option><option>8</option>
      <option>9</option><option>10</option>
    </select >
    <input class="cancel" type="button" value="cancel" />
   </td>
   <td>More string</td><td>More string</td>
   <td>More string</td><td>More string</td>
   <td>More string</td><td>More string</td>
</tr>
{% endfor %}
</script>

<script>
  var data = [];

  for (var i = 0; i < 100; i++) {
    var row = {
      id: i,
      firstName: 'john',
      lastName: 'doe'
    };

    data.push(row);
  }

  $.template("savedTmpl", document.getElementById("tmplRow").innerHTML);

  var underscoreTmpl = "\
    <%for(var i=0;i<rows.length;i++){%>\
    <%var row=rows[i];%> \
    <tr> \
      <td> <input type = 'checkbox' value = '<%= row.id%>' /> </td>\
      <td><%= row.firstname%> <%= row.lastname%></td> \
      <td class = 'edit'> \
        <a> Edit </a>\
        <input style='display:none;' type='hidden' value='Blah' /> \
        <input class = 'cancel' type = 'button' value = 'cancel' /> \
      </td>\
      <td class='select'>\
          <a>Select</a> \
          <select style = 'display:none;'> \
            <option>0</option>\
            <option>1</option> \
            <option>2</option>\
            <option>3</option> \
            <option>4</option>\
            <option>5</option> \
            <option>6</option>\
            <option>7</option> \
            <option>8</option>\
            <option>9</option> \
            <option>10</option>\
           </select> \
            <input class = 'cancel' type = 'button' value = 'cancel' /> \
       </td>\
       <td>More string</td> \
       <td>More string</td>\
       <td>More string</td> \
       <td>More string</td>\
       <td>More string</td> \
       <td>More string</td>\
    </tr ><%}%>";

  var mustacheTmpl = "\
    {{#rows}}\
    <tr> \
      <td> <input type = 'checkbox' value = '{{id}}' /> </td>\
      <td>{{firstname}} {{lastname}}</td> \
      <td class = 'edit'> \
        <a> Edit </a>\
        <input style='display:none;' type='hidden' value='Blah' /> \
        <input class = 'cancel' type = 'button' value = 'cancel' /> \
      </td>\
      <td class='select'>\
          <a>Select</a> \
          <select style = 'display:none;'> \
            <option>0</option>\
            <option>1</option> \
            <option>2</option>\
            <option>3</option> \
            <option>4</option>\
            <option>5</option> \
            <option>6</option>\
            <option>7</option> \
            <option>8</option>\
            <option>9</option> \
            <option>10</option>\
           </select> \
            <input class = 'cancel' type = 'button' value = 'cancel' /> \
       </td>\
       <td>More string</td> \
       <td>More string</td>\
       <td>More string</td> \
       <td>More string</td>\
       <td>More string</td> \
       <td>More string</td>\
    </tr >{{/rows}}"

  var milkTmpl = "\
    {{#rows}}\
    <tr> \
      <td> <input type = 'checkbox' value = '{{id}}' /> </td>\
      <td>{{firstname}} {{lastname}}</td> \
      <td class = 'edit'> \
        <a> Edit </a>\
        <input style='display:none;' type='hidden' value='Blah' /> \
        <input class = 'cancel' type = 'button' value = 'cancel' /> \
      </td>\
      <td class='select'>\
          <a>Select</a> \
          <select style = 'display:none;'> \
            <option>0</option>\
            <option>1</option> \
            <option>2</option>\
            <option>3</option> \
            <option>4</option>\
            <option>5</option> \
            <option>6</option>\
            <option>7</option> \
            <option>8</option>\
            <option>9</option> \
            <option>10</option>\
           </select> \
            <input class = 'cancel' type = 'button' value = 'cancel' /> \
       </td>\
       <td>More string</td> \
       <td>More string</td>\
       <td>More string</td> \
       <td>More string</td>\
       <td>More string</td> \
       <td>More string</td>\
    </tr >{{/rows}}"

  var jsRenderTmpl = "\
    <tr> \
      <td> <input type = 'checkbox' value = '{{=id}}' /> </td>\
      <td>{{=firstname}} {{=lastname}}</td> \
      <td class = 'edit'> \
        <a> Edit </a>\
        <input style='display:none;' type='hidden' value='Blah' /> \
        <input class = 'cancel' type = 'button' value = 'cancel' /> \
      </td>\
      <td class='select'>\
          <a>Select</a> \
          <select style = 'display:none;'> \
            <option>0</option>\
            <option>1</option> \
            <option>2</option>\
            <option>3</option> \
            <option>4</option>\
            <option>5</option> \
            <option>6</option>\
            <option>7</option> \
            <option>8</option>\
            <option>9</option> \
            <option>10</option>\
           </select> \
            <input class = 'cancel' type = 'button' value = 'cancel' /> \
       </td>\
       <td>More string</td> \
       <td>More string</td>\
       <td>More string</td> \
       <td>More string</td>\
       <td>More string</td> \
       <td>More string</td>\
    </tr >";

var handlebarsTmpl = Handlebars.compile($("#handlebarsTmpl").html());

swig.init({
    autoescape: false,
    cache: true,
    filters: {}
});
var swigTmpl = swig.compile($("#swigTmpl").html());

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery templates
var html1 = $("#tmplRow").tmpl(data)
ready
Precompiled jQuery template
var html3 = $.tmpl("savedTmpl", data);
ready
Underscore.js
var untpl = _.template(underscoreTmpl);
var html4 = untpl({
  rows: data
});
ready
mustache js
var html = Mustache.to_html(mustacheTmpl, {
  rows: data
});
ready
jsrender
var html = $.render(data, jsRenderTmpl);
ready
ICanHaz.js
var html = ich.icanhazTmpl({
  rows: data
});
ready
Handlebars.js
var html = handlebarsTmpl({
  rows: data
});
ready
Milk
var html = Milk.render(milkTmpl, {
  rows: data
});
ready
swig
var html = swigTmpl({rows: data});
 
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.