JavaScript template language shootoff (v773)

Revision 773 of this benchmark created on


Description

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

Added Hogan, Dust and EJSpeed, removed functional dupes or edge cases

Preparation HTML

<script src="http://linkedin.github.com/dustjs/dist/dust-full-1.0.0.js"></script>
<script src="http://documentcloud.github.com/underscore/underscore.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.0/mustache.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.rc.1/handlebars.min.js"></script>
<script src="http://opheleon.com/ejspeed-noswitch.js"></script>
<script src="http://opheleon.com/ejs.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/hogan.js/2.0.0/hogan.js"></script>
<script>
  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.handlebarsTemplate = 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.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.sharedVariables = {
   header: "Header",
   header2: "Header2",
   header3: "Header3",
   header4: "Header4",
   header5: "Header5",
   header6: "Header6",
   list: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
  };
    
//dust
(function(){dust.register("dusttmp",body_0);function body_0(chk,ctx){return chk.write("<h1 class='header'>").reference(ctx.get("header"),ctx,"h").write("</h1><h2 class='header2'>").reference(ctx.get("header2"),ctx,"h").write("</h2><h3 class='header3'>").reference(ctx.get("header3"),ctx,"h").write("</h3><h4 class='header4'>").reference(ctx.get("header4"),ctx,"h").write("</h4><h5 class='header5'>").reference(ctx.get("header5"),ctx,"h").write("</h5><h6 class='header6'>").reference(ctx.get("header6"),ctx,"h").write("</h6>");}return body_0;})();  
  
window.hoganTemplate = Hogan.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'>{{#list}}<li class='item'>{{{.}}}</li>{{/list}}</ul></div>");

// EJSpeed, removed "with", "Array.push"
  window.ejspeed = new EJSpeed({
    text: micro2
  });

// EJS - original
  window.ejs = new EJS({
    text: micro
  });

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Mustache.js Template
Mustache.to_html(mustacheTemplate, sharedVariables);
ready
Underscore.js Template
underscoreTemplate(sharedVariables);
ready
Handlebars.js
handlebarsTemplate(sharedVariables);
ready
EJSpeed
ejspeed.render(sharedVariables);
ready
EJS
ejs.render(sharedVariables)
ready
Hogan
hoganTemplate.render(sharedVariables);
ready
Dust
dust.render('dusttmp', sharedVariables, function() {});
ready

Revisions

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