Templating in Knockout.js (v13)

Revision 13 of this benchmark created on


Preparation HTML

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

Test runner

Ready to run.

Testing in
TestOps/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]);
ready
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]);
ready
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]);
ready

Revisions

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