Object vs Array

JavaScript performance comparison

Test case created by Greggman

Info

See if array is faster than object

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var ObjectVector = function(x, y, z) {
      this.x = x;
      this.y = y;
      this.z = z;
    };
   
    ObjectVector.prototype.add = function(other) {
      this.x += other.x;
      this.y += other.y;
      this.z += other.z;
    };
   
   
    var ArrayVector = function(x, y, z) {
      this.array = [x, y, z];
    };
   
    ArrayVector.prototype.add = function(other) {
      this.array[0] += other.array[0];
      this.array[1] += other.array[1];
      this.array[2] += other.array[2];
    };
   
    var ov1 = new ObjectVector(1,2,3);
    var ov2 = new ObjectVector(4,5,6);
    var av1 = new ArrayVector(1,2,3);
    var av2 = new ArrayVector(4,5,6);
};
</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
Using Object
ov1.add(ov2);
pending…
Using Array
av1.add(av2);
pending…

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

Compare results of other browsers

0 comments

Add a comment