JS Template Engines Rendering (v164)

Revision 164 of this benchmark created by Nikos M. on


Description

raw, non-html-escaped, template output used in the tests

Preparation HTML

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="http://mustache.github.io/extras/mustache.js"></script>
<script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v2.0.0.js"></script>
<script src="http://kendo.cdn.telerik.com/2014.2.716/js/kendo.ui.core.min.js"></script>
<script src="http://olado.github.io/doT/doT.min.js"></script>
<script src="http://foo123.github.io/libs/Contemplate-1.0.0.min.js"></script>

<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);
}

  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']
  };

/* use  a common .render method interface to even things up between engines, measure pure rendering performance and not method calling overhead */

window.underscoreTpl = {render:_.template("<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.mustacheTpl = "<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.handlebarsTpl = {render:Handlebars.compile("<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.resigTpl = {render:tmpl("<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.resig2Tpl = {render:tmpl2("<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.kendouiTpl = {render:kendo.template("<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>", {useWithBlock: false})};

window.dotTpl = {render: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>")};
  
// use common configuration options as rest engines
// 1. no preserve lines in output
// 2. use  template as standalone, no dynamic context manipulation
Contemplate.add({'test': ["<# #>\n"+
"<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($item in $list) #><li class='item'><# $item #></li><# endfor #></ul></div>"]});
Contemplate.setPreserveLines( false );
window.contemplateTpl = Contemplate.tpl('test', null, {standalone:true});

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
mustache 0.8.1
Mustache.render( mustacheTpl, sharedVariables );
ready
handlebars 2.0.0
handlebarsTpl.render( sharedVariables );
ready
underscore 1.8.3
underscoreTpl.render( sharedVariables );
ready
Resig Template (no "with" block)
resig2Tpl.render( sharedVariables );
ready
Contemplate 1.0.0
contemplateTpl.render( sharedVariables );
ready
KendoUI 2014.2.716
kendouiTpl.render( sharedVariables );
ready
doT 1.0.3
dotTpl.render( sharedVariables );
ready

Revisions

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