1D & 2D arrays

JavaScript performance comparison

Test case created by cGuille

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var twoDArr = [];
    for (var i = 0; i < 100; ++i) {
      var arr = [];
      for (var j = 0; j < 100; ++j) {
        arr.push(i + j);
      }
      twoDArr.push(arr);
    }
   
    var oneDArr= [];
    for (var i = 0; i < 100; ++i) {
      for (var j = 0; j < 100; ++j) {
        oneDArr.push(i + j);
      }
    }
};
</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
1D for loop
var i;

for (i = 0; i < 10000; ++i) {
  oneDArr[i];
}
pending…
2D for loop without cache
var i, j;

for (i = 0; i < 100; ++i) {
  for (j = 0; j < 100; ++j) {
    twoDArr[i][j];
  }
}
pending…
2D for loop with cache
var i, j, cache;

for (i = 0; i < 100; ++i) {
  cache = twoDArr[i];
  for (j = 0; j < 100; ++j) {
    cache[j];
  }
}
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