array of objects vs array of numbers

JavaScript performance comparison

Revision 3 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var point = function(x, y) {
      this.X = x;
      this.Y = y;
    }
    var point_array = [new point(10, 20), new point(10, 20), new point(10, 20), new point(10, 20), new point(10, 20), new point(10, 20), new point(10, 20), new point(10, 20), new point(10, 20), new point(10, 20)];
    var point_array2 = new Array(10);
    var point_array3 = new Array(new point(10, 20), new point(10, 20), new point(10, 20), new point(10, 20), new point(10, 20), new point(10, 20), new point(10, 20), new point(10, 20), new point(10, 20), new point(10, 20));
   
    var x_array = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10];
    var y_array = [20, 20, 20, 20, 20, 20, 20, 20, 20, 20];
    var x_array2 = new Array(10);
    var y_array2 = new Array(10);
    var x_array3 = new Array(10, 10, 10, 10, 10, 10, 10, 10, 10, 10);
    var y_array3 = new Array(20, 20, 20, 20, 20, 20, 20, 20, 20, 20);
   
    var len = x_array.length;
    var temp = 0;
};
</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
RW point array
for (var i = 0; i < len; i++) {
  point_array2[i] = new point(point_array[i].X + 1, point_array[i].Y + 1);
}
pending…
RW xy array
for (var i = 0; i < len; i++) {
  x_array2[i] = x_array[i] + 1;
  y_array2[i] = y_array[i] + 1;
}
pending…
RW refilled point array
for (var i = 0; i < len; i++) {
  point_array3[i].X = point_array[i].X + 1
  point_array3[i].Y = point_array[i].Y + 1;
}
pending…
RW prefilled xy array
for (var i = 0; i < len; i++) {
  x_array3[i] = x_array[i] + 1;
  y_array3[i] = y_array[i] + 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