From this post:
I would first suggest that you optimize your
dependentObservable
(a.k.a.computed
). When you read any observable, Knockout registers a dependency to it in Dependency Manager...I can see in your pseudo-code that you are accessing this.ParentList() in the while loop. It means registerDependency will be called 3000 times and the dependencies array will be scanned 3000 times, which is bad for IE (since it has no built-in Array.indexOf method).
So my number one suggestion would be: Read all observables before loops.
With this good advice in mind, here's my question:
Is it bad form to iterate through observable
arrays with ko utility methods (like the following)? [assume that this.mySelectListItems()
is an observableArray
]:
self.selectedValuesCount = ko.computed(function () {
var total = 0;
ko.utils.arrayForEach(this.mySelectListItems(), function (item) {
if (item.selected() === true) {
total += 1;
}
});
return total;
}, self);
In other words, is there anything to be gained by doing the following?
self.selectedValuesCount = ko.computed(function () {
var total = 0;
var myArray = this.mySelectListItems();
ko.utils.arrayForEach(myArray, function (item) {
if (item.selected() === true) {
total += 1;
}
});
return total;
}, self);