Templating in Knockout.js
JavaScript performance comparison
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.
| Test | Ops/sec | |
|---|---|---|
jquery template plugin (no data-bind) |
|
pending… |
jquery template plugin |
|
pending… |
KO native templating |
|
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:
- Revision 1: published by Scott Messinger
- Revision 2: published
- Revision 3: published by Scott Messinger
- Revision 5: published by Scott Messinger
- Revision 6: published by Scott Messinger
- Revision 7: published by Ryan Niemeyer
- Revision 8: published by Michael Best
- Revision 9: published by Michael Best
- Revision 12: published
- Revision 13: published
- Revision 14: published
- Revision 16: published
- Revision 17: published
- Revision 18: published
- Revision 19: published
- Revision 20: published
- Revision 21: published
- Revision 22: published
- Revision 23: published by thelinuxlich
0 comments