Properties vs. Indices

JavaScript performance comparison

Test case created by Pipo

Info

Check what is faster: Properties or indices?

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var playerObj = {
      pos: {
        x: 0,
        y: 0
      }
    };
   
    var Point = function(x, y) {
      this.x = x;
      this.y = y;
    };
   
    var playerObjWithConstructor = {
      pos: new Point(0, 0)
    };
   
    var playerArray = {
      pos: [0, 0]
    }
   
    var tmpStorage = 0,
      POS_X = 0,
      POS_Y = 1;
};
</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
Object with Properties
playerObj.pos.x = Math.random();
tmpStorage = playerObj.pos.x;

playerObj.pos.y = Math.random();
tmpStorage = playerObj.pos.y;
pending…
Array with Indices
playerArray.pos[0] = Math.random();
tmpStorage = playerArray.pos[0];

playerArray.pos[1] = Math.random();
tmpStorage = playerArray.pos[1];
pending…
Array with Indices and Constants
playerArray.pos[POS_X] = Math.random();
tmpStorage = playerArray.pos[POS_X];

playerArray.pos[POS_Y] = Math.random();
tmpStorage = playerArray.pos[POS_Y];
pending…
Object with Properties and a Constructor
playerObjWithConstructor.pos.x = Math.random();
tmpStorage = playerObjWithConstructor.pos.x;

playerObjWithConstructor.pos.y = Math.random();
tmpStorage = playerObjWithConstructor.pos.y;
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