JavaScript template language shootoff (v1029)

Revision 1029 of this benchmark created by SerzN1 on


Description

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

Note: When adding a new test, please ensure that your test returns the same HTML string (or equivalent DOM fragment) as the others.

Preparation HTML

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="http://documentcloud.github.com/underscore/underscore.js">
</script>
<script src="http://rawgit.com/janl/mustache.js/master/mustache.js">
</script>
<script src="http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.0.beta.6.js">
</script>

 //Resig Template Function (modified to support ')
function tmpl(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);
  }



//Resig modified template function (no "with" block)

  function tmpl2(str) {
    var strFunc = "var p=[];" + "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("data", strFunc);
  }



<script src="http://rawgit.com/raw/860240/cd98cacbdeee7eb2cfb2ca3ca76638dae2a5b1af/micro3.js">
</script>
<script src="http://rawgit.com/olado/doT/master/doT.js">
</script>
<script src="http://akdubya.github.com/dustjs/dist/dust-full-0.3.0.min.js">
</script>
<script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>


<script>

 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.micro = "<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.micro2 = "<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.underscoreTemplate = _.template(micro);

  window.resigTemplate = tmpl(micro);
  window.resigTemplate2 = tmpl2(micro2);
  window.resigTemplate3 = tmpl3(micro2);

 

  window.doTtemplate = doT.template("<div><h1 class='header'>{{= it.header }}</h1><h2 class='header2'>{{= it.header2 }}</h2><h3 class='header3'>{{= it.header3 }}</h3><h4 class='header4'>{{= it.header4 }}</h4><h5 class='header5'>{{= it.header5 }}</h5><h6 class='header6'>{{= it.header6 }}</h6><ul class='list'>{{ for (var i = 0, l = it.list.length; i < l; i++) { }}<li class='item'>{{= it.list[i] }}</li>{{ } }}</ul></div>");


</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Mustache.js Template
window.Mustache.to_html(mustacheTemplate, sharedVariables);
ready
Resig Micro-Templating.
//resigTemplate(sharedVariables);
window.tmpl(micro, sharedVariables)
ready
Resig Micro-Templating 2.
//resigTemplate2(sharedVariables);
window.tmpl2(micro2, sharedVariables);
ready
Resig Micro-Templating 3.
//resigTemplate3(sharedVariables);
window.tmpl3(micro2, sharedVariables);
ready
Underscore.js Template
window.underscoreTemplate(sharedVariables);
ready
jQuery Templates
window.jQueryTemplate($, {
  data: sharedVariables
}).join("");
ready
Handlebars.js
window.handlebarsTemplate(sharedVariables);
ready
doT.js
window.doTtemplate(sharedVariables);
ready

Revisions

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