JS Template Engines Performance

JavaScript performance comparison

Revision 7 of this test case created

Info

Preparation code

<script src="http://sb.taurus.uberspace.de/jslib/jquery-1.7.1.min.js">
</script>
<script src="http://sb.taurus.uberspace.de/jslib/underscore-1.3.1.min.js">
</script>
<script src="http://twitter.github.com/hogan.js/builds/1.0.5/hogan-1.0.5.js">
</script>
<script src="http://sb.taurus.uberspace.de/jslib/jquery.mustache-0.5.0.min.js">
</script>
<script src="http://sb.taurus.uberspace.de/jslib/jquery.tmpl.min.js">
</script>
<script src="http://sb.taurus.uberspace.de/jslib/handlebars.1.0.beta.5.js">
</script>
<script src="http://cdn.kendostatic.com/2011.3.1129/js/kendo.core.min.js">
</script>
<script src="http://sb.taurus.uberspace.de/jslib/dust-full-0.3.0.min.js">
</script>
<script src="http://tempojs.com/tempo.js">
</script>
<script src="http://twitter.github.com/hogan.js/builds/1.0.5/hogan-1.0.5.js">
</script>
<!--External Template Definitions-->
<script type="text/x-kendo-template" id="kendoUIextTemplate">
  < 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>
</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.hoganTemplate = "<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.hoganCompile = Hogan.compile(window.hoganTemplate);


  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.underscoreTemplate = _.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.jQueryTemplate = $.template(null, "<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.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>");


  Handlebars.template((function() {
    var a = Handlebars.template,
        b = Handlebars.templates = Handlebars.templates || {};
    b.handlebarsTemplatePrecompiled = a(function(a, b, c, d, e) {
      function p(a, b) {
        var c = "",
            d;
        return c += "<li class='item'>", d = a, typeof d === l ? d = d.call(a, {
          hash: {}
        }) : d === n && (d = m.call(a, "this", {
          hash: {}
        })), c += o(d) + "</li>", c
      }
      c = c || a.helpers;
      var f = "",
          g, h, i, j, k = this,
          l = "function",
          m = c.helperMissing,
          n = void 0,
          o = this.escapeExpression;
      f += "<div><h1 class='header'>", i = c.header, g = i || b.header, typeof g === l ? g = g.call(b, {
        hash: {}
      }) : g === n && (g = m.call(b, "header", {
        hash: {}
      })), f += o(g) + "</h1><h2 class='header2'>", i = c.header2, g = i || b.header2, typeof g === l ? g = g.call(b, {
        hash: {}
      }) : g === n && (g = m.call(b, "header2", {
        hash: {}
      })), f += o(g) + "</h2><h3 class='header3'>", i = c.header3, g = i || b.header3, typeof g === l ? g = g.call(b, {
        hash: {}
      }) : g === n && (g = m.call(b, "header3", {
        hash: {}
      })), f += o(g) + "</h3><h4 class='header4'>", i = c.header4, g = i || b.header4, typeof g === l ? g = g.call(b, {
        hash: {}
      }) : g === n && (g = m.call(b, "header4", {
        hash: {}
      })), f += o(g) + "</h4><h5 class='header5'>", i = c.header5, g = i || b.header5, typeof g === l ? g = g.call(b, {
        hash: {}
      }) : g === n && (g = m.call(b, "header5", {
        hash: {}
      })), f += o(g) + "</h5><h6 class='header6'>", i = c.header6, g = i || b.header6, typeof g === l ? g = g.call(b, {
        hash: {}
      }) : g === n && (g = m.call(b, "header6", {
        hash: {}
      })), f += o(g) + "</h6><ul class='list'>", i = c.list, g = i || b.list, h = c.each, j = k.program(1, p, e), j.hash = {}, j.fn = j, j.inverse = k.noop, g = h.call(b, g, j);
      if (g || g === 0) f += g;
      return f += "</ul></div>\n", f
    })
  })());

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

  window.resig = 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>");

  //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.resig2 = 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.kendouiTemplate = 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: true
  });

  window.kendouiTemplate2 = 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
  });

  //Use external template definition
  window.kendoUIAlt1 = kendo.template($("#kendoUIextTemplate").html());
  window.kendoUIAlt2 = kendo.template($("#kendoUIextTemplate").html(), {
    useWithBlock: false
  });

  window.dustTemplate1 = function(content) {
    dust.loadSource(dust.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>", "dustTemplate1"));
    dust.render("dustTemplate1", content, function() {});
  };

  window.dustTemplatePrecompiled = (function() {
    dust.register("dustTemplatePrecompiled", 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;
  })();

  window.dustTemplate2 = function(content) {
    dust.loadSource(dustTemplatePrecompiled, "dustTemplatePrecompiled");
    dust.render("dustTemplatePrecompiled", content, function() {});
  };

  window.dustTemplatePrecompiledCached = (function() {
    dust.register("dustTemplatePrecompiledCached", 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, "dustTemplatePrecompiledCached");

  window.dustTemplate3 = function(content) {
    dust.render("dustTemplatePrecompiledCached", content, function() {});
  };


  $(document.body).append('<ul id="tempoTemplate"> <li data-template> {{header}} {{header2}} {{header3}} {{header4}} {{header5}} {{header6}} <ul><li >{{list}}</li></ul></li></ul> ');


  var tempoPrepared = Tempo.prepare('tempoTemplate');
  tempoTemplate = function(data) {
    tempoPrepared.render(data);
  }

  tempoTemplate();

</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(mustacheTemplate, sharedVariables);
pending…
underscore.js
underscoreTemplate(sharedVariables);
pending…
jquery.tmpl.js
jQueryTemplate($, {
  data: sharedVariables
}).join("");
pending…
handlebar.js
handlebarsTemplate(sharedVariables);
pending…
handlebar.js (precompiled)
Handlebars.templates.handlebarsTemplatePrecompiled(sharedVariables);
pending…
dust.js
//dustTemplate1(sharedVariables);
pending…
dust.js (precompiled)
//dustTemplate2(sharedVariables);
pending…
dust.js (precompiled, cached)
//dustTemplate3(sharedVariables);
pending…
TempoJs
//tempoTemplate(sharedVariables);
pending…
Hoganjs
hoganCompile.render(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. Here’s a list of current revisions for this page:

0 comments

Add a comment