object vs array

JavaScript performance comparison

Revision 3 of this test case created by aaronps

Preparation code

<script>
  var obj = {
   x: 1,
   y: 2
  };
 
  var arr = [1, 2];
 
  function Obj_1(_x, _y) {
   this.x = _x;
   this.y = _y
  }
 
  var nob = new Obj_1(1, 2);
</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 create
var o = {
 x: 1,
 y: 2
};
pending…
array create
var a = [1, 2];
pending…
obj access
obj.x = 3;
obj.y = 4;
var x = obj.x;
var y = obj.y;
pending…
array access
arr[0] = 3;
arr[1] = 4;
var x = arr[0];
var y = arr[1];
pending…
array string access
arr['0'] = 3;
arr['1'] = 4;
var x = arr['0'];
var y = arr['1'];
pending…
new object
var o = new Obj_1(1, 2)
pending…
new object access
nob.x = 1;
nob.y = 2;
var x = nob.x;
var y = nob.y;
pending…
new object string access
nob['x'] = 1;
nob['y'] = 2;
var x = nob['x'];
var y = nob['y'];
pending…
object string access
obj['x'] = 3;
obj['y'] = 4;
var x = obj['x'];
var y = obj['y'];
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