JavaScript template language shootoff (v942)

Revision 942 of this benchmark created by Lance Taylor on


Description

EDIT: Moved template compilation into the tests for fair comparison of all operations. Preperations now contain only string templates.

A limited comparison of some popular JavaScript templating engines on a short template: 6 header tags, and 10 list items. Compared templating engines:

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/ext-core/3/ext-core.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<script src="http://documentcloud.github.com/underscore/underscore.js"></script>

<!--NOTE: Need to load from CDNJS since GitHub uses text/plain MIME type, which is blocked in IE9-->
<!--<script src="http://github.com/janl/mustache.js/raw/master/mustache.js"></script>-->
<script src="http://ajax.cdnjs.com/ajax/libs/mustache.js/0.3.0/mustache.min.js"></script>

<script src="https://github.com/downloads/wycats/handlebars.js/handlebars.1.0.0.beta.3.js"></script>

<script src="http://cdn.kendostatic.com/2011.2.804/js/kendo.all.min.js"></script>

<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"></script>

<!--NOTE: Need to load from MSFT CDN since GitHub uses text/plain MIME type, which is blocked in IE9-->
<!--<script src="http://github.com/jquery/jquery-tmpl/raw/master/jquery.tmpl.min.js"></script>-->
<script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>

<script src="http://qatrix.com/js/qatrix-1.1.min.js"></script>
<script src="http://www.lance-taylor.com/MyTemplate.js"></script>

Setup

var $ = jQuery.noConflict();
      window.sharedVariables = {
       header: "Header",
       header2: "Header2",
       header3: "Header3",
       header4: "Header4",
       header5: "Header5",
       header6: "Header6",
       list: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
      };
      
      window.mustacheTemplate = "<div><h1 class='header'>{{{header}}}</h1><h2 class='header2'>{{{header2}}}</h2><h3 class='header3'>{{{header3}}}</h3><h4 class='header4'>{{{header4}}}</h4><h5 class='header5'>{{{header5}}}</h5><h6 class='header6'>{{{header6}}}</h6><ul class='list'>{{#list}}<li class='item'>{{{.}}}</li>{{/list}}</ul></div>";
    
      window.jQueryTemplate = "<div><h1 class='header'>{{html header}}</h1><h2 class='header2'>{{html header2}}</h2><h3 class='header3'>{{html header3}}</h3><h4 class='header4'>{{html header4}}</h4><h5 class='header5'>{{html header5}}</h5><h6 class='header6'>{{html header6}}</h6><ul class='list'>{{each list}}<li class='item'>{{html $value}}</li>{{/each}}</ul></div>"
      
      window.kendouiTemplate = "<div><h1 class='header'><#= data.header #></h1><h2 class='header2'><#= data.header2 #></h2><h3 class='header3'><#= data.header3 #></h3><h4 class='header4'><#= data.header4 #></h4><h5 class='header5'><#= data.header5 #></h5><h6 class='header6'><#= data.header6 #></h6><ul class='list'><# for (var i = 0, l = data.list.length; i < l; i++) { #><li class='item'><#= data.list[i] #></li><# } #></ul></div>";
    
      window.handlebarsTemplate = "<div><h1 class='header'>{{header}}</h1><h2 class='header2'>{{header2}}</h2><h3 class='header3'>{{header3}}</h3><h4 class='header4'>{{header4}}</h4><h5 class='header5'>{{header5}}</h5><h6 class='header6'>{{header6}}</h6><ul class='list'>{{#each list}}<li class='item'>{{this}}</li>{{/each}}</ul></div>";
    
      window.underscoreTemplate = "<div><h1 class='header'><%= header %></h1><h2 class='header2'><%= header2 %></h2><h3 class='header3'><%= header3 %></h3><h4 class='header4'><%= header4 %></h4><h5 class='header5'><%= header5 %></h5><h6 class='header6'><%= header6 %></h6><ul class='list'><% for (var i = 0, l = list.length; i < l; i++) { %><li class='item'><%= list[i] %></li><% } %></ul></div>";
      
      window.baseHtml = "<div><h1 class='header'></h1><h2 class='header2'></h2><h3 class='header3'></h3><h4 class='header4'></h4><h5 class='header5'></h5><h6 class='header6'></h6><ul class='list'><li class='item'></li></ul></div>";
      
      //Resig Template Function (modified to support ')
      window.tmpl = function(str) {
                  var strFunc =
                  "var p=[];" +
                              "with(obj){p.push('" +
      
                  str.replace(/[\r\t\n]/g, " ")
                     .replace(/'(?=[^#]*#>)/g, "\t")
                     .split("'").join("\\'")
                     .split("\t").join("'")
                     .replace(/<#=(.+?)#>/g, "',$1,'")
                     .split("<#").join("');")
                     .split("#>").join("p.push('")
                     + "');}return p.join('');";
      
                  return new Function("obj", strFunc);
              }
      
      window.resig = "<div><h1 class='header'><#= header #></h1><h2 class='header2'><#= header2 #></h2><h3 class='header3'><#= header3 #></h3><h4 class='header4'><#= header4 #></h4><h5 class='header5'><#= header5 #></h5><h6 class='header6'><#= header6 #></h6><ul class='list'><# for (var i = 0, l = list.length; i < l; i++) { #><li class='item'><#= list[i] #></li><# } #></ul></div>";
    
    window.qatrixTemplate = "<div><h1 class='header'>{{header}}</h1><h2 class='header2'>{{header2}}</h2><h3 class='header3'>{{header3}}</h3><h4 class='header4'>{{header4}}</h4><h5 class='header5'>{{header5}}</h5><h6 class='header6'>{{header6}}</h6><ul class='list'>{{#list}}<li class='item'>{{this}}</li>{{/list}}</ul></div>";
    
     /**
        * Template generator compiler.
        * @author stefano.azzolini@caffeinalab.com
        * @url http://caffeinalab.com/
        */
        
        window.template_compiler = function(template) {
          return new Function('o',
            'o=o||{};return "' + template
            .replace(/"/g, '\\"')
            .split('{')
            .join('"+(o.')
            .split('}')
            .join('||"")+"') + '";'
          );
        }
    
    window.fastTemplate = "<div><h1 class='header'>{header}</h1><h2 class='header2'>{header2}</h2><h3 class='header3'>{header3}</h3><h4 class='header4'>{header4}</h4><h5 class='header5'>{header5}</h5><h6 class='header6'>{header6}</h6><ul class='list'></ul></div>";
    
    window.myTemplate = '<div><h1 class="header" data-dom-text="header"></h1><h2 class="header2" data-dom-text="header2"></h2><h3 class="header3" data-dom-text="header3"></h3><h4 class="header4" data-dom-text="header4"></h4><h5 class="header5" data-dom-text="header5"></h5><h6 class="header6" data-dom-text="header6"></h6><ul class="list"><li class="item" data-dom-repeat="list" data-dom-text="$val"></li></ul></div>';

Test runner

Ready to run.

Testing in
TestOps/sec
Mustache
Mustache.to_html(mustacheTemplate, sharedVariables);
ready
jQuery.template
$.template(null, jQueryTemplate)($, {
  data: sharedVariables
}).join("");
ready
Kendo UI
kendo.template(kendouiTemplate, {useWithBlock:true})(sharedVariables);
ready
Handlebars
Handlebars.compile(handlebarsTemplate)(sharedVariables);
ready
Underscore
_.template(underscoreTemplate)(sharedVariables);
ready
Resig micro-templating (mod)
tmpl(resig)(sharedVariables)
ready
Qatrix
Qatrix.$template(qatrixTemplate)(sharedVariables);
ready
fastTemplate (does not loop li tag)
template_compiler(fastTemplate)(sharedVariables);
ready
myTemplate
MyTemplate.buildTemplate(myTemplate, sharedVariables);
ready

Revisions

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