Templating in Knockout.js

JavaScript performance comparison

Revision 13 of this test case created

Preparation code

<table id="main"></table>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://github.com/jquery/jquery-tmpl/blob/master/jquery.tmpl.js">
</script>
<script src="https://github.com/SteveSanderson/knockout/raw/master/build/output/knockout-latest.debug.js">
</script>
<script id="rowTmpl" type="text/html">
   <tr data-bind="template: { name: 'cellTmpl', foreach: cells }"></tr>
</script>

<script id="cellTmpl" type="text/html">
   <td data-bind="text: 'Cell ' + id()"></td>
</script>

<script id="jqTmpl" type="text/html">
   {{each rows}}
   <tr>
      {{each cells}}
         <td>Cell ${id}</td>
      {{/each}}
   </tr>
   {{/each}}
</script>

<script>
function Cell(id) {
    this.id = ko.observable(id);  
}
function Row(id) {
    this.id = ko.observable(id);
    this.cells = ko.observableArray();
}

var viewModel = {
    rows: ko.observableArray()
};

for (var i = 0; i < 100; i++) {
    var row = new Row(i);
   
    for (var j = 0; j < 10; j++) {
        row.cells.push(new Cell(i + " - " + j));
    }
    viewModel.rows.push(row);
}
</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
jquery template plugin (no data-bind)
ko.setTemplateEngine(new ko.jqueryTmplTemplateEngine());

var main = $("#main");
main.attr("data-bind", "template: 'jqTmpl'");

ko.utils.emptyDomNode(main[0]);
ko.applyBindings(viewModel, main[0]);
pending…
jquery template plugin
ko.setTemplateEngine(new ko.jqueryTmplTemplateEngine());

var main = $("#main");
main.attr("data-bind", "template: { name: 'rowTmpl', foreach: rows }");

ko.utils.emptyDomNode(main[0]);
ko.applyBindings(viewModel, main[0]);
pending…
KO native templating
ko.setTemplateEngine(ko.nativeTemplateEngine.instance);

var main = $("#main");
main.attr("data-bind", "template: { name: 'rowTmpl', foreach: rows }");

ko.utils.emptyDomNode(main[0]);
ko.applyBindings(viewModel, main[0]);
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