JavaScript template engine compare

JavaScript performance comparison

Revision 85 of this test case created by ysugimoto

Info

Precompiled (to check non-compiled, goto /69, or comment 1 line in tests, and uncomment 2nd/3rd)

Preparation code

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<script src="http://rawgithub.com/spmbt/doT/master/doT.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
<script src="http://borismoore.github.com/jsrender/jsrender.js"></script>
<script src="http://github.com/janl/mustache.js/raw/master/mustache.js"></script>
<script src="https://rawgithub.com/atmajs/MaskJS/master/lib/mask.min.js"></script>
<script src="http://ysugimoto.github.io/Retriever/build/retriever.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0-alpha.2/handlebars.min.js"></script>

<!--External Template Definitions-->
<script>
  window.sharedVariables = {
   header0: "Test render template engine ",
   header2: "Header2",
   header3: "Header3",
   header4: "Header4",
   header5: "Header5",
   header6: "Header6",
   list: ['1000000', '2000000', '3000000', '4000000', '5000000', '6000000', '7000000', '8000000', '9000000', '10']
  };

window.doTtemplateRaw = "<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>";
window.doTtemplate = doT.compile(doTtemplateRaw);

 
window.underscoreTemplateRaw = "<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.underscoreTemplate = _.template(underscoreTemplateRaw);
 
window.underscoreTemplateNoWithRaw = "<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.underscoreTemplateNoWith = _.template(underscoreTemplateNoWithRaw, null, {variable: 'data'});

  //JsRender compiled template (no encoding)
window.jsRenderTemplateRaw = "<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 list}}<li class='item'>{{:#data}}</li>{{/for}}</ul></div>";
window.jsRenderTemplate = $.templates(jsRenderTemplateRaw);
 
window.mustacheTemplateRaw = "<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.retrieverTemplateRaw = "<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'>{{loop list}}<li class='item'>{{@data}}</li>{{/loop}}</ul></div>";
window.retrieverTemplate = Retriever.make(retrieverTemplateRaw);

window.HandlebarsTemplateRaw = "<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.HandlebarsTemplate = Handlebars.compile(HandlebarsTemplateRaw);

window.maskTemplateRaw = "div { \
        h1.header > '~[header]'\
        h2.header2 > '~[header2]'\
        h3.header3 > '~[header3]'\
        h4.header4 > '~[header4]'\
        h5.header5 > '~[header5]'\
        h6.header6 > '~[header6]'\
        ul > each (list) > li > '~[.]'\
    }"
;
  window.maskDom = mask.compile(maskTemplateRaw)
 
</script>

<div id="template" style=""></div>
<script>
Benchmark.prototype.setup = function() {
    template = $('#template').get(0);
};
</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
1.doT.js
template.innerHTML = '';

sharedVariables.header = sharedVariables.header0 +'doT.js';
template.innerHTML = doTtemplate(sharedVariables);

++sharedVariables.list[0];
pending…
2.Underscore
template.innerHTML = '';

sharedVariables.header = sharedVariables.header0 +'Underscore';
template.innerHTML = underscoreTemplate(sharedVariables);

++sharedVariables.list[1];
pending…
3.Underscore (no "with")
template.innerHTML = '';

sharedVariables.header = sharedVariables.header0 +'Underscore (no "with")';
template.innerHTML = underscoreTemplateNoWith(sharedVariables);

++sharedVariables.list[2];
pending…
4.JsRender
template.innerHTML = '';

sharedVariables.header = sharedVariables.header0 +'JsRender';
template.innerHTML = jsRenderTemplate.render(sharedVariables);

++sharedVariables.list[3];
pending…
6.MaskJs
template.innerHTML = '';

sharedVariables.header = sharedVariables.header0 +'MaskJs';
template.appendChild(mask.render(maskTemplateRaw, sharedVariables));

++sharedVariables.list[5];
pending…
7.doT Compile
template.innerHTML = '';

sharedVariables.header = sharedVariables.header0 +'doT Compile';
template.innerHTML = doT.compile(doTtemplateRaw)(sharedVariables);

++sharedVariables.list[6];
pending…
8. Retriever
template.innerHTML = '';

sharedVariables.header = sharedVariables.header0 +'Retriever';
template.innerHTML = retrieverTemplate.parse(sharedVariables);

++sharedVariables.list[7];
pending…
9.Handlebars(compiled)
template.innerHTML = '';

sharedVariables.header = sharedVariables.header0 +'Handlebars';
template.innerHTML = HandlebarsTemplate(sharedVariables);

++sharedVariables.list[8];
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