Render Template From DOM Select vs Cache

JavaScript performance comparison

Revision 4 of this test case created

Info

This test was spawned by some discussion in a StackOverflow thread:

http://stackoverflow.com/questions/9833312/how-do-i-properly-store-a-javascript-template-so-that-it-isnt-instantiated-mul

The purpose is to test the speed of rendering an underscore.js template when selecting the template from the DOM every time, vs selecting it once and re-rendering the cached template every time.

Preparation code

<script id="test-template" type="text/template">
  <p>
    this is <div class="foo">some html</div>
  </p>
  <ul>
    <li>with data
    <li>and items
    <li>etc
  </ul>
</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="//cdn.kendostatic.com/2011.3.1129/js/kendo.all.min.js"></script>
<div id="html-output-goes-here"></div>
<script>
Benchmark.prototype.setup = function() {
    this.cachedItem = $("#test-template");
    this.cachedTemplate = _.template(this.cachedItem.html());
    this.output = $("#html-output-goes-here");
    this.kendoCachedTemplate = kendo.template(this.cachedItem.html());
    this.kendoCachedTemplateWithoutBlock = kendo.template(this.cachedItem.html(), {useWithBlock:false});
};

Benchmark.prototype.teardown = function() {
    delete this.cachedItem;
    delete this.cachedTemplate;
    delete this.kendoCachedTemplate
    delete this.kendoCachedTemplateWithoutBlock
};
</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
_ Cached
var html = this.cachedTemplate();
this.output.html(html);
pending…
Non-Cached
var uncachedItem = $("#test-template");
var uncachedTemplate = _.template(uncachedItem.html());
var html = uncachedTemplate();
this.output.html(html);
pending…
kendo cached
var html = this.kendoCachedTemplate({});
this.output.html(html);
pending…
Kendo No With cached
var html = this.kendoCachedTemplateWithoutBlock ({});
this.output.html(html);
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