ValueHasMutated vs push apply

Benchmark created on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/2.1.0/knockout-min.js">
</script>
<ul data-bind="foreach: items">
  <li data-bind="text: text">
  </li>
</ul>
​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Test runner

Ready to run.

Testing in
TestOps/sec
ValueHasMutated
function ViewModel() {
  var self = this;
  this.items = ko.observableArray([]);
  this.addItems = function(items) {
    [].push.apply(self.items(), items);
    self.items.valueHasMutated();
  };
}

function Item(text) {
  this.text = ko.observable(text);
}


var vm = new ViewModel();
ko.applyBindings(vm);
var i = 0;
var items = [];

for (i = 0; i < 200; i++) {
  items.push(new Item("Item" + i));
}
vm.addItems(items);​
ready
push
function ViewModel() {
  var self = this;
  this.items = ko.observableArray([]);
  this.addItems = function(items) {
    self.items.push.apply(self.items, items);
  };
}

function Item(text) {
  this.text = ko.observable(text);
}


var vm = new ViewModel();
ko.applyBindings(vm);
var i = 0;
var items = [];

for (i = 0; i < 200; i++) {
  items.push(new Item("Item" + i));
}
vm.addItems(items);​
ready

Revisions

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