octane-remove

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function OrderedCollection() {
      this.elms = new Array();
    }
   
    OrderedCollection.prototype.add = function(elm) {
      this.elms.push(elm);
    }
   
    OrderedCollection.prototype.size = function() {
      return this.elms.length;
    }
   
    var o = new OrderedCollection();
    for (var i = 0; i < 500; i++) {
      o.add(i);
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
original
OrderedCollection.prototype.remove = function(elm) {
  var index = 0,
      skipped = 0;
  for (var i = 0; i < this.elms.length; i++) {
    var value = this.elms[i];
    if (value != elm) {
      this.elms[index] = value;
      index++;
    } else {
      skipped++;
    }
  }
  for (var i = 0; i < skipped; i++)
  this.elms.pop();
}

for (var k = 0; k < 500; k += 2) {
  o.remove(k);
}
pending…
filter
OrderedCollection.prototype.remove = function(elm) {
  this.elms = this.elms.filter(function(v) {
    return elm !== v;
  });
}

for (var k = 0; k < 500; k += 2) {
  o.remove(k);
}
pending…
splice
OrderedCollection.prototype.remove = function(elm) {
  for (var i = this.elms.length - 1; i >= 0; i--) {
    if (this.elms[i] === elm) this.elms.splice(i, 1);
  }
}

for (var k = 0; k < 500; k += 2) {
  o.remove(k);
}
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. Here’s a list of current revisions for this page:

0 comments

Add a comment