Handlebars Compile, Compile+Cache, Precompile, Precompile+Cache (v13)

Revision 13 of this benchmark created on


Preparation HTML

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v1.3.0.js"></script>

<script id="template" type="text/plain">
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>
 <div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
  {{#each items}}
    <div>{{text}}</div>
  {{/each}}
 </div>

</script>

<script>
var source = $("#template").html();
var compiled = Handlebars.compile(source);
var precompiled;
eval('precompiled = '+Handlebars.precompile(source));
var precompiledCached = Handlebars.template(precompiled);

var testData = {
  title: 'handlebars',
  body: 'test',
  items: [
    { text: 'test1' },
    { text: 'test2' },
    { text: 'test3' },
    { text: 'test4' }
  ]
};

function testCompile() {
  var fn = Handlebars.compile(source);
  fn(testData);
}

function testCompileAndCache() {
  compiled(testData);
}

function testPrecompile() {
  var fn = Handlebars.template(precompiled);
  fn(testData);
}

function testPrecompileAndCache() {
  precompiledCached(testData);
}
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Compile
testCompile();
ready
Compile+Cache
testCompileAndCache();
ready
testPrecompile
testPrecompile();
ready
testPrecompileAndCache
testPrecompileAndCache();
ready

Revisions

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