Hadlebars vs. Hogan vs. Mustache vs. jsRender vs Dust vs Underscore vs Micro-templates vs xtpl (v67)

Revision 67 of this benchmark created by rmaksim on


Description

Illustrate how much time string-based template engines use executing browsers HTML parser.

Optimizing string interpolation performance is focusing to 10% of the problem, not the 90%.

Added micro-templates by John Resig and xtpl - it's optimized version

Added etmpl - optimized John Resig micro-templates

Preparation HTML

<div id="target"></div>
<script src="http://fb.me/react-with-addons-0.11.1.js"></script>
<script src="http://fb.me/JSXTransformer-0.11.1.js"></script>
<script type="text/jsx">
 /** @jsx React.DOM */
  var ReactComponent = React.createClass({
    render: function (){
      var object = this.props.object;
      var list = object.list.map(function (e, i){
        return <li key={i}>{e}</li>
      })
      return (
        <div>
          {object.template}
          <h1 class='header'>{object.header}</h1>
          <h2 class='header2'>{object.header2}</h2>
          <h3 class='header3'>{object.header3}</h3>
          <h4 class='header4'>{object.header4}</h4>
          <h5 class='header5'>{object.header5}</h5>
          <h6 class='header6'>{object.header6}</h6>
          <ul class='list'>
            {list}
          </ul>
        </div>      
      )
    }
});
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<script src="http://mustache.github.io/extras/mustache.js"></script>

<script src="https://github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.min.js"></script>

<script src="http://jsdo.it/kyo_ago/6M93/js"></script>

<script src="http://akdubya.github.io/dustjs/dist/dust-full-0.3.0.js"></script>

<script src="http://borismoore.github.io/jsrender/jsrender.js"></script>

<script src="http://twitter.github.com/hogan.js/builds/2.0.0/hogan-2.0.0.js"></script>

<script src= "//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js" ></script>

<script type="text/tmpl" id="micro-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'>
<% var i = 0, len = list.length; for ( ; i < len; i++ ) { %>
    <li class='item'><%= list[i] %></li>
  <% } %></ul>
</div>
</script>
<script>
  window.mustacheTemplate = "<div>{{template}}<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>{{template}}<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'>{{this}}</li>{{/list}}</ul></div>");
  
  window.hoganTemplate = Hogan.compile("<div>{{template}}<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.templateTemplate = "<div><{template}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'>{{loop list}}<li class='item'>{{this}}</li>{{/loop}}</ul></div>";

  $.templates({ jsRenderTemplate: "<div>{{:template}}<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 list}}<li class='item'>{{:#data}}</li>{{/for}}</ul></div>"}); 

  dust.loadSource(dust.compile("<div>{template}<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>", "intro"));

  window.underscoreTemplate = _.template("<div><%=template%><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 in list){%><li class='item'><%=list[i]%></li><% } %></ul></div>");


      window.tmpl = function tmpl(str, data){
        // Figure out if we're getting a template, or if we need to
        // load the template - and be sure to cache the result.
        var fn =           
          // Generate a reusable function that will serve as a template
          // generator (and which will be cached).
          new Function("obj",
            "var p=[],print=function(){p.push.apply(p,arguments);};" +
            
            // Introduce the data as local variables using with(){}
            "with(obj){p.push('" +
            
            // Convert the template into pure JavaScript
            str
              .replace(/[\r\t\n]/g, " ")
              .split("<%").join("\t")
              .replace(/((^|%>)[^\t]*)'/g, "$1\r")
              .replace(/\t=(.*?)%>/g, "',$1,'")
              .split("\t").join("');")
              .split("%>").join("p.push('")
              .split("\r").join("\\'")
          + "');}return p.join('');");
        
        // Provide some basic currying to the user
        return data ? fn( data ) : fn;
      };

window.microTemplate = tmpl('<div><%=template%><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 in list){%><li class="item"><%=list[i]%></li><% } %></ul></div>');

window.etmpl = function(str, data){

            str = str
                .replace(/[\r\n]/g ,      "")
                .replace(/\\'/g,          "'")
                .replace(/'/g,            "\\'")
                .replace(/<\?=(.*?)\?>/g, "'+($1)+'")
                .replace(/<\?(.*?)\?>/g,  "';$1;s+='")
            str = "s='';s+='" + str + "';return s;"

        var fn = new Function("data", "s", str)
        return data ? fn(data) : fn
    }

window.etmplTemplate = etmpl('<div><?=data.template?><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 in data.list){?><li class="item"><?=data.list[i]?></li><? } ?></ul></div>');


//== xtpl
    (function(){this.xtpl=function xtpl(templatestring,data){var ret='',err='';try{var rdata=/{%=\s*(.+?)\s*%}/g,rfunc=/{%=\s*\(\s*(.+?)\s*\)\s*%}/g,xfn=new Function("xtplData","var arr=[]; arr.push('"+templatestring.replace(/[\r\t\v\n\b]/g,"").split("'").join("\\'").split("{%js").join("');").replace(rfunc,"',(xtplData.$1),'").replace(rdata,"',(xtplData.$1)?xtplData.$1:'','").split("js%}").join("; arr.push('")+"'); return arr.join('');")}catch(err){txt="xtpl: There is an error inside the template string\n\n";txt+="Click OK to continue.\n\n";alert(txt);return''}if(!data){return xfn}try{ret=xfn(data)}catch(err){txt="xtpl: Error occured when generating the HTML from the template \n\n";txt+="Click OK to continue.\n\n";alert(txt);return''}return ret}})();

window.xtplTemplate = xtpl('<div>{%=template%}<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>{%=xxxx%}<h6 class="header6">{%=header6%}</h6><ul class="list">{%js var i=0, alen=xtplData.list.length; for ( i=0; i < alen; i++ ) { js%} <li class="item">{%=list[i]%}</li> {%js } js%}</ul></div>');

</script>

Setup

window.sharedVariables = {
        header: "Header",
        header2: "Header2",
        header3: "Header3",
        header4: "Header4",
        header5: "Header5",
        header6: "Header6",
        list: ['10000000', '2', '3', '4', '5', '6', '7', '8', '9', '10']
      };

Teardown


    //$("#target").html('');
  

Test runner

Ready to run.

Testing in
TestOps/sec
DOM - Mustache.js Template
sharedVariables.template = 'Mustache.js';
Mustache.parse(mustacheTemplate);
document.getElementById("target").innerHTML = Mustache.render(mustacheTemplate, sharedVariables);
++sharedVariables.list[0];
ready
DOM - Twitter's Hogan.js
sharedVariables.template = 'Hogan.js';
document.getElementById("target").innerHTML = hoganTemplate.render(sharedVariables);
++sharedVariables.list[0];
ready
DOM - Handlebars.js
sharedVariables.template = 'Handlebars.js';
document.getElementById("target").innerHTML = handlebarsTemplate(sharedVariables);
++sharedVariables.list[0];
ready
DOM - jsRender
sharedVariables.template = 'jsRender';
document.getElementById("target").innerHTML = $.render.jsRenderTemplate(sharedVariables);
++sharedVariables.list[0];
ready
DOM - dust.js
// async test
sharedVariables.template = 'dust.js';
dust.render("intro", sharedVariables, function(err, out) {
  document.getElementById("target").innerHTML = out;
  deferred.resolve();
});
++sharedVariables.list[0];
ready
DOM - template.js
sharedVariables.template = 'template.js';
document.getElementById("target").innerHTML = template(templateTemplate, sharedVariables);
++sharedVariables.list[0];
ready
Underscore
sharedVariables.template = 'underscore.js';
document.getElementById("target").innerHTML = window.underscoreTemplate(sharedVariables);
++sharedVariables.list[0];
ready
Micro-template
sharedVariables.template = 'microtemplate';
document.getElementById("target").innerHTML = window.microTemplate(sharedVariables);
++sharedVariables.list[0];
ready
xtpl
sharedVariables.template = 'xtpl';
document.getElementById("target").innerHTML = window.xtplTemplate(sharedVariables);
++sharedVariables.list[0];
ready
ReactJS
// async test
sharedVariables.template = 'ReactJS';
React.renderComponent(
  ReactComponent({
    object: sharedVariables
  }),
  document.getElementById("target"),
  function() {
    deferred.resolve()
  }
);
++sharedVariables.list[0];
ready
etmpl
sharedVariables.template = 'etmpl';
document.getElementById("target").innerHTML = window.etmplTemplate(sharedVariables);
++sharedVariables.list[0];
ready

Revisions

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