Templating in Knockout.js

JavaScript performance comparison

Revision 7 of this test case created by Ryan Niemeyer

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/raw/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

<table id="main"></table> <script src="https://github.com/jquery/jquery-tmpl/raw/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>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
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.

0 Comments