point array vs point object

JavaScript performance comparison

Test case created by Timo

Preparation code

<script>
var point_o = function(x,y) {
this.X=x;
  this.Y=y;
}
var point_a = function(x,y) {
  return [x,y];
}
var point_o_arr = [];
var point_a_arr = [];
var x,y;
var len=100;
for(var i=0;i<len;i++)
{
  x = Math.random()*10000;
  y = Math.random()*10000;
  point_o_arr.push(new point_o(x,y));
  point_a_arr.push(point_a(x,y));  
}

//console.log(JSON.stringify(point_o_arr));
//console.log(JSON.stringify(point_a_arr));
</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
R object
// R object
for(var i=0;i<len;i++)
{
  x = point_o_arr[i].X + 1;
  y = point_o_arr[i].Y + 1;
}
 
pending…
R array
// R array
for(var i=0;i<len;i++)
{
  x = point_a_arr[i][0] + 1;
  y = point_a_arr[i][1] + 1;
}
 
pending…
W object
// W object
for(var i=0;i<len;i++)
{
  point_o_arr[i].X = 1;
  point_o_arr[i].Y = 1;
}
 
pending…
W array
// W array
for(var i=0;i<len;i++)
{
  point_a_arr[i][0] = 1;
  point_a_arr[i][1] = 1;
}

 
pending…
RW object
// RW object
for(var i=0;i<len;i++)
{
  point_o_arr[i].X = point_o_arr[i].Y + 1;
  point_o_arr[i].Y = point_o_arr[i].X + 1;
}
 
pending…
RW array
// RW array
for(var i=0;i<len;i++)
{
  point_a_arr[i][0] = point_a_arr[i][1] + 1;
  point_a_arr[i][1] = point_a_arr[i][0] + 1;

}
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