DOM vs innerHTML based Templating (v828)

Revision 828 of this benchmark created on


Description

A brief comparison of some JavaScript templating engines on a short template: 7 DOM nodes ... 7 interpolated values.

Preparation HTML

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

<script src="http://beebole.com/pure/wp-content/themes/BeeBole-pure/libs/pure.js"></script>

<script src="http://documentcloud.github.com/underscore/underscore.js"></script>

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


<div class="purejs_template"><div><h1 class="header"></h1><div class='a'></div><div class='b'><div class='c'><div class='d'><div class='e'><div class='f'></div></div></div></div></div></div></div>

<script>
  window.mustacheTemplate = "<div><h1 class='header'>{{header}}</h1><div class='a'>{{a}}</div><div class='b'>{{b}}<div class='c'>{{c}}<div class='d'>{{d}}<div class='e'>{{e}}<div class='f'>{{f}}</div></div></div></div></div></div>";

  window.glassesTemplate = '<div><h1 class="header">{{header}}</h1><div class="a">{{a}}</div><div class="b">{{b}}<div class="c">{{c}}<div class="d">{{d}}<div class="e">{{e}}<div class="f">{{f}}</div></div></div></div></div></div>';
  
  window.underscoreTemplate = _.template("<div><h1 class='header'>#{header}</h1><div class='a'>#{a}</div><div class='b'>#{b}<div class='c'>#{c}<div class='d'>#{d}<div class='e'>#{e}<div class='f'>#{f}</div></div></div></div></div></div>");
  
  window.sharedVariables = {
   header: "Header",
   a: 'aaa',
   b: 'bbb',
   c: 'ccc',
   d: 'ddd',
   e: 'eee',
   f: 'fff'
  };

window.Glasses = function(model, template) {
  var _lr, _render, _rr, _string;
  if (!window.glassesTemplateFunction) {
    _lr = /{{/g;
    _rr = /}}/g;
    _string = template.replace(_lr, "'+model.").replace(_rr, "+'");
    _render = Function.apply(null, ['model', "return '" + _string + "'"]);
    window.glassesTemplateFunction = _render;
  } else {
    _render = window.glassesTemplateFunction;
  }
  return _render(model);
};

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Mustache.js Template
Mustache.to_html(mustacheTemplate, sharedVariables);
ready
Pure JS Template
$('.purejs_template').autoRender(sharedVariables);
ready
Underscore.js Template
underscoreTemplate(sharedVariables);
ready
Glasses.coffee Template
Glasses(sharedVariables, glassesTemplate);
ready

Revisions

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