Vanilla vs Custom Array

JavaScript performance comparison

Revision 4 of this test case created

Info

Testing different ways of appending methods to Array

Preparation code

<script>
  function remove(obj) {
    var i = this.indexOf(obj);
    if (i >= 0) {
      this.splice(i, 1);
      return true;
    }
    return false;
  }
 
  Array.prototype.remove = remove;
 
  var vanilla = [];
 
  var chocolate = Object.create(Array.prototype, {
    remove: {
      value: remove,
      enumerable: false
    }
  });
 
  var mint = [];
  mint.remove = remove;
 
  for (var i = 0; i < 10; i++) {
    vanilla.push({
      name: i
    });
    chocolate.push({
      name: i
    });
    mint.push({
      name: i
    });
  }
 
  var thing = {
    name: "mr thing"
  };
</script>
<script>
Benchmark.prototype.setup = function() {
    vanilla.push(thing);
    chocolate.push(thing);
    mint.push(thing);
};

Benchmark.prototype.teardown = function() {
    vanilla.remove(thing);
    chocolate.remove(thing);
    mint.remove(thing);
};
</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
Vanilla Array
remove.call(vanilla, thing);
pending…
Custom Array (Object.create)
chocolate.remove(thing);
pending…
Custom Array (.remove =)
mint.remove(thing);
pending…
Custom Prototype
vanilla.remove(thing);
pending…
Resolve remove (Object.create)
chocolate.remove
pending…
Resolve remove (Custom Prototype
vanilla.remove
pending…
Resolve remove (.remove =)
mint.remove
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