Javascript performance. Array vs Object.

Big JavaScript application needs big storage. JS gives us 2 options Array and Object. I have tested following cases:

  • writing data
  • writing data randomly
  • reading data
  • reading data randomly

I have tested this in following browsers:

  • IE
  • FF
  • Chrome

I want to point out that i am not comparing browsers. I am comparing two methods of storing data in JavaScript.

I have checked perfomance of following pices of code:

writing [ ]

// initialization
var r=[];
//code to evaluate
for (i=0;i<10000;i++){
    this.r[i]=7;
}

writing {}

// initialization
var r={};
//code to evaluate
for (i=0;i<10000;i++){
    this.r[i]=7;
}

writing [ ] randomly

// initialization
var r=[];
//code to evaluate
for (i=0;i<10000;i++){
    this.r[Math.floor(Math.random(1000))]=7;
}

writing { } randomly

// initialization
var r={};
//code to evaluate
for (i=0;i<10000;i++){
    this.r[Math.floor(Math.random(1000))]=7;
}

Here are results in ms:

  IE6 FF3.5 Chrome3
writing [] 14.85 0.95 0.6
writing {} 14.05 4.3 0.65
writing [] randomly 36.7 4.1 1.55
writing {} randomly 37.5 6.15 1.6
reading [] 12 0.2 0.4
reading {} 13.3 1.5 0.35
reading [] randomly 71.05 57.2 53.05
reading {} randomly 71.1 48.9 53.65

Ok the result shows us that the perfomance of Array and Object as storage is almost equal.
So semantically i think it is wise to use array for unordered information and object for mapping.

This entry was posted in post and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>