Performance of Array vs. Object (v138)

Revision 138 of this benchmark created on


Description

After seeing http://jsperf.com/javascript-associative-vs-non-associative-arrays, I thought the test could be improved.

Setup

function makeid(len){
        var text = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    
        for( var i=0; i < len; i++ )
            text += possible.charAt(Math.floor(Math.random() * possible.length));
    
        return text;
    }
    
    var arr1 = [], arr2 =[], obj = {};
    
    for(i = 0; i < 100; i += 1) {
    var str1 = makeid(50);
    var str2 = makeid(50);     
    arr1.push(str1);
    arr2.push(str2);
    obj[str1] = str2;
    }
    
    var key = arr1[Math.floor(100*Math.random())];

Test runner

Ready to run.

Testing in
TestOps/sec
Array Performance
for(i = 0; i < 100; i += 1) {
var index = arr1.indexOf(key);
var str = arr2[index];
}
 
ready
Object Performance
for(i = 0; i < 100; i += 1) {
var str = obj[key];
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.