Get from object vs array

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj = {1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10};
    var arr = new Array(1,2,3,4,5,6,7,8,9,10);
   
    function getFromArray(){
        for(var i = 0; i < 10; i++){
                for(var i = 0; i < arr.length; i++){
                        if(i == arr[i]){
                                var val = arr[i];
                                break;
                        }
                }
        }
    }
   
    function getFromObject(){
        for(var i = 0; i < 10; i++){
                var val = obj[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
Get from object
getFromObject();
pending…
Get from array
getFromArray();
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