In the simple example below, Knockout calls HTMLElement.appendChild 18 times to render the template. It doesn't use DocumentFragment, so each of these 18 operations are made on the live DOM causing reflows. Ideally, there should be only one call to appendChild on the live DOM.
This really hurts performance, does anyone know how to reduce the damage?
JS BIN with the code.
JavaScript
var viewModel = {
people:[
{name: 'Tim'},
{name: 'John'},
{name: 'Greg'}
]
};
ko.applyBindings(viewModel, document.getElementById('list1'));
HTML
<ul id='list1' data-bind="foreach: { data: people }">
<li>
<h3 data-bind="text: name"></h3>
</li>
</ul>
liit will not have to deal with those text nodes (so<ul><li></li></ul>). That brings it down to 10. The 1st one is actually copying the originallito a newdivas part of storing it, so not appending to the document. The last 3 are unrelated to KO (just part of loading jQuery). So, there are only 6 (adding 3lis and 3 text nodes fortextbinding. - RP Niemeyer