Splice vs filter

JavaScript performance comparison

Test case created by Splice vs filter

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  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;
          }
      }
  };

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Push
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

array.removeIf1(function(item) {
    return item > 3;
});
pending…
Filter
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

array.filter(function(item) {
    return item <= 3;
});
pending…
Splice
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

array.removeIf2(function(item) {
    return item > 3;
});
pending…

Compare results of other browsers

Revisions

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

0 Comments