ArrayVsObject

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function Keys() {
        this.items = {};
        this.find = function(key) {
            return this.items[key];
        };
        this.push = function(key, value) {
            this.items[key] = new UnnamedItem(value);
        }
    }
    function ItemArray() {
        this.items = [
        ];
        this.find = function(key) {
            for(var i = 0; i < this.items.length; i++) {
                if(this.items[i].name === key) { return this.items[i]; }
            }
            return null;
        }
        this.push = function(key, value) {
            this.items.push(new NamedItem(key, value));
        }
    }
    function NamedItem(name, value) {
        this.name = name;
        this.value = value;
    }
    function UnnamedItem(value) {
        this.value = value;
    }
   
    var k = new Keys()
    for(var i = 0; i < 1000; i++) {
        k.push('k' + i, i);
    }
   
    var a = new ItemArray()
    for(var i = 0; i < 1000; i++) {
        a.push('k' + i, i);
    }
};
</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
Object
k.find('k42');
pending…
Array
a.find('k42');
pending…
Object travel
var e;
for (var key in k.items)
  e = k.items[key];

 
pending…
Array travel
var e;
for (var i=0,len = k.items.length;i<len;i++)
  e = k.items[i];
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