ValueHasMutated vs push apply (v3)

Revision 3 of this 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>
​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Setup

function ViewModel() {
      var self = this;
      this.items = ko.observableArray([]);
      this.addItems1 = function(items) {
        [].push.apply(self.items(), items);
        self.items.valueHasMutated();
      };
    
      this.addItems2 = function (items) {
          self.items.push.apply(self.items, items);
      };
    }
    
    function Item(text) {
      this.text = ko.observable(text);
    }
    
    var vm = new ViewModel();
    ko.applyBindings(vm);

Teardown


    vm.items([]);
  

Test runner

Ready to run.

Testing in
TestOps/sec
ValueHasMutated
var i = 0;
var items = [];

for (i = 0; i < 2000; i++) {
  items.push(new Item("Item" + i));
}
vm.addItems1(items);
ready
push
var i = 0;
var items = [];

for (i = 0; i < 2000; i++) {
  items.push(new Item("Item" + i));
}
vm.addItems2(items);
ready

Revisions

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