Vanilla vs Custom Array

JavaScript performance comparison

Revision 3 of this test case created by Ryan Alexander

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
    });
  }
 
  var thing = {
    name: "mr 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
vanilla.push(thing);
remove.call(vanilla, thing);
pending…
Custom Array (Object.create)
chocolate.push(thing);
chocolate.remove(thing);
pending…
Custom Array (.remove =)
mint.push(thing);
mint.remove(thing);
pending…
Custom Prototype
vanilla.push(thing);
vanilla.remove(thing);
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