DOM vs innerHTML based Templating

JavaScript performance comparison

Revision 327 of this test case created


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

Preparation code

<script src=""></script>
<script src=""></script>
<script src=""></script>

<script src=""></script>

<script src=""></script>

<script src=""></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>

  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.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'

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 Template
Mustache.to_html(mustacheTemplate, sharedVariables);
Pure JS Template
Underscore.js Template

Compare results of other browsers


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:


Mic commented :

You are using the autoRender of pure.js which is very slow, as it parse the HTML to find the classes, and compile the template every time. While underscore seems already compiled.

Again these tests are relevant if you do server rendering. If you plan to use a templating engines on the browser, there is no value in being 100ms instead of 500ms to run all the transformation.

pure.js is about have the HTML clean, and is fast enough for the heaviest client rendering.

Mic commented :

If you target browser rendering, there is another bias. You compare template engines that output strings, and DOM.

This test doesn't take into account the insertion of the HTML string in the DOM.

Comment form temporarily disabled.

Add a comment