if/else vs arrays vs switch vs ternary (v29)

Revision 29 of this benchmark created on


Description

checks performance of the many types of case iterators in javascript.

Preparation HTML

<script>
function Switch( x ) {
        switch ( x ) {
                case 100: return 10; break;
                case 200: return 18448; break;
                case 400: return 188888; break;
                case 500: return 166; break;
        }       
}
var testArray = [];

testArray[100] = 10;
testArray[200] = 18448;
testArray[400] = 188888;
testArray[500] = 166;

function arrays(x){  
        return testArray[x];
}

</script>

Setup

function Switch( x ) {
            switch ( x ) {
                    case 100: return 10; break;
                    case 200: return 18448; break;
                    case 400: return 188888; break;
                    case 500: return 166; break;
            }       
    }
    
    var testArray = [];
    
    testArray[100] = 10;
    testArray[200] = 18448;
    testArray[400] = 188888;
    testArray[500] = 166;
    
    function arrays(x){  
            return testArray[x];
    }

Test runner

Ready to run.

Testing in
TestOps/sec
switch
Switch(400);
ready
indexed arrays
arrays(400);
ready

Revisions

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