Array Performance

JavaScript performance comparison

Revision 101 of this test case created by tuan

Preparation code

<script>
  Benchmark.prototype.setup = function() {
    var a1 = [];
    for (var i = 0; i < 2; i++) {
       a1[i] = {
         id: i,
         name: 'test'
       };
    }

    var a2 = {};
    for (var i = 0; i < 2; i++) {
       a2[i] = {
         id: 'a' + i,
         name: 'test'
       };
    }

    var a3 = {};
    for (var i = 0; i < 2; i++) {
       a3['a' + i] = {
         id: 'a' + i,
         name: 'test'
       };
    }


  };
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Array query
var id = 1;
for (var i = 0; i < a1.length; i++) {
  if (a1[i].id === id) result = a1[i];
}
pending…
Obj query
var id = 1;
var result = a2[id];
pending…
obj (key is string) query
var id = 'a1';
var result = a3[id];
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.

0 Comments