Push vs Splice vs Filter (v2)

Revision 2 of this benchmark created on


Setup

var arrayLength = 10000;
  
  var comparator = function(item) {
    return item < arrayLength;
  };
  
  var getArray = function() {
    var array = [];
  
    for (var i = 0; i < arrayLength; ++i) {
      array.push(i);
    }
    return array;
  }
  
  Array.prototype.removeIf1 = function(expression) {
    var res = [];
    for (var idx = 0; idx < this.length; idx++) {
      var currentItem = this[idx];
      if (!expression(currentItem)) {
        res.push(currentItem);
      }
    }
    return res;
  };
  
  Array.prototype.removeIf2 = function(callback) {
    var i = 0;
    while (i < this.length) {
      if (callback(this[i])) {
        this.splice(i, 1);
      } else {
        ++i;
      }
    }
  };

Test runner

Ready to run.

Testing in
TestOps/sec
Push
var array = getArray();

array.removeIf1(comparator);
ready
Filter
var array = getArray();

array.filter(comparator);
ready
Splice
var array = getArray();

array.removeIf2(comparator);
ready

Revisions

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