Book Image

Mastering KnockoutJS

By : Timothy Moran
Book Image

Mastering KnockoutJS

By: Timothy Moran

Overview of this book

Table of Contents (16 chapters)
Mastering KnockoutJS
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Performance concerns


Knockout's performance has improved several times since its initial release, but it is still possible to encounter issues in apps with a large number of operations or objects. While some decrease in performance should be expected as the work being done increases, there are ways to ease the burden on the CPU.

Observable loops

Changing observable arrays inside loops causes them to publish change notifications multiple times. The cost of these changes is proportional to the size of the array. You might need to add several items to an array and use a loop to do this:

var contacts = ko.observableArray();
for (var i = 0, j = newContacts.length; i < j; i++) {
    contacts.push(new Contact(newContacts[i]);
}

The problem here is that push gets called multiple times, which causes the array to send out multiple change notifications. It's much easier for the subscribers of the array if all of the changes are sent at once. This can be done by collecting all of the changes in the loop...