JavaScript template language shootoff

JavaScript performance comparison

Revision 942 of this test case created by Lance Taylor

Preparation code

<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>
      
<script>
Benchmark.prototype.setup = function() {
  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>';

};
</script>

Preparation code output

<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>

Test runner

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

Java applet disabled.

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

Compare results of other browsers

Revisions

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

0 Comments