Template Engines Dust JS / Handlebar JS / Mustache JS

JavaScript performance comparison

Revision 8 of this test case created by Alex Liu

Preparation code

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>

<script src="//cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.2/mustache.min.js"></script>

<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.min.js"></script>

<script src="//cdnjs.cloudflare.com/ajax/libs/dustjs-linkedin/2.0.0/dust-core.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.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'>{{.}}</li>{{/each}}</ul></div>";

// add handlebars precompiled template
  (function() {
  var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['foobar'] = template(function (Handlebars,depth0,helpers,partials,data) {
  this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); data = data || {};
  var buffer = "", stack1, functionType="function", escapeExpression=this.escapeExpression, self=this;

function program1(depth0,data) {
 
  var buffer = "";
  buffer += "<li class='item'>"
    + escapeExpression((typeof depth0 === functionType ? depth0.apply(depth0) : depth0))
    + "</li>";
  return buffer;
  }

  buffer += "<div><h1 class='header'>";
  if (stack1 = helpers.header) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
  else { stack1 = depth0.header; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
  buffer += escapeExpression(stack1)
    + "</h1><h2 class='header2'>";
  if (stack1 = helpers.header2) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
  else { stack1 = depth0.header2; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
  buffer += escapeExpression(stack1)
    + "</h2><h3 class='header3'>";
  if (stack1 = helpers.header3) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
  else { stack1 = depth0.header3; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
  buffer += escapeExpression(stack1)
    + "</h3><h4 class='header4'>";
  if (stack1 = helpers.header4) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
  else { stack1 = depth0.header4; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
  buffer += escapeExpression(stack1)
    + "</h4><h5 class='header5'>";
  if (stack1 = helpers.header5) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
  else { stack1 = depth0.header5; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
  buffer += escapeExpression(stack1)
    + "</h5><h6 class='header6'>";
  if (stack1 = helpers.header6) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
  else { stack1 = depth0.header6; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
  buffer += escapeExpression(stack1)
    + "</h6><ul class='list'>";
  stack1 = helpers.each.call(depth0, depth0.list, {hash:{},inverse:self.noop,fn:self.program(1, program1, data),data:data});
  if(stack1 || stack1 === 0) { buffer += stack1; }
  buffer += "</ul></div>";
  return buffer;
  });
})();

  window.dustTemplatePrecompiledCached = (function(){dust.register("foobar",body_0);function body_0(chk,ctx){return chk.write("<div><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><ul class=\"list\">").section(ctx.get("list"),ctx,{"block":body_1},null).write("</ul></div>");}function body_1(chk,ctx){return chk.write("<li class=\"item\">").reference(ctx.getPath(true,[]),ctx,"h").write("</li>");}return body_0;})();

    dust.loadSource(dustTemplatePrecompiledCached);

</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
mustache.js
Mustache.render(mustacheTemplate, sharedVariables);
pending…
handlebar.js
Handlebars.compile(handlebarsTemplate)(sharedVariables);
pending…
handlebar.js (precompiled)
Handlebars.templates.foobar(sharedVariables)
pending…
linkedindust.js (precompiled)
dust.render("foobar", sharedVariables, function(){});
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. Here’s a list of current revisions for this page:

0 comments

Add a comment