JavaScript template language shootoff (v79)

Revision 79 of this benchmark created by aefxx on


Description

A comparison of the more performant/capeable contestants in the wide field of JavaScript templating solutions. I've narrowed down the competition to six templating engines, which are: eco, jQote2, underscore, handlebars, doT, and doU.

We'll be measuring the performance on a very simple template including 6 header tags and 10 list items.

EDIT Some scripts hosted on github won't load in IE9 RC due to false MIME type headers. I therefore am hosting these on my server - files are identical to the ones at github.

Preparation HTML

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"></script>
<script src="http://sstephenson.github.com/eco/dist/eco.js"></script>
<script src="http://aefxx.com/files/jquery.jqote2.js"></script>
<script src="http://documentcloud.github.com/underscore/underscore.js"></script>
<script src="http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-0.9.0.pre.3.js"></script>
<script src="http://aefxx.com/files/doT.js"></script>
<script src="http://aefxx.com/files/doU.js"></script>
<script src="http://aefxx.com/files/json2.js"></script>

<script>
  window.shared = {
   header: "Header",
   header2: "Header2",
   header3: "Header3",
   header4: "Header4",
   header5: "Header5",
   header6: "Header6",
   list: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
  };
  window.contestant = {};
  c = window.contestant;
  
  c.eco = eco("<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 item in @list: %><li class='item'><%- item %></li><% end %></ul></div>");
  
  c.jQote2 = $.jqotec("<div><h1 class='header'><%= this.header %></h1><h2 class='header2'><%= this.header2 %></h2><h3 class='header3'><%= this.header3 %></h3><h4 class='header4'><%= this.header4 %></h4><h5 class='header5'><%= this.header5 %></h5><h6 class='header6'><%= this.header6 %></h6><ul class='list'><% for (var n = 0, l = this.list.length; n < l; n++) { %><li class='item'><%= this.list[n] %></li><% } %></ul></div>");
  
  c.underscore = _.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>");
  
  c.handlebars = 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>");
  
  c.doT = doT.template("<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>");
  
  c.doU = doU.template("<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>");
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
eco.js
contestant.eco(shared);
ready
jQote2
contestant.jQote2.call(shared);
ready
Underscore.js
contestant.underscore(shared);
ready
Handlebars.js
contestant.handlebars(shared);
ready
doT.js
contestant.doT(shared);
ready
doU.js
contestant.doU(shared);
ready

Revisions

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