IF statement vs CASE Statement

JavaScript performance comparison

Test case created by newbreedofgeek

Info

When compared to IF statements, CASE statement are known to be more effcient in a lot of programming languages. But at work we have realised that when we change all our CASE statements to IF statement the application performance a lot faster.

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
IF Statement
function testThis(lookFor) {
        var returnMatchedValue;

    if (lookFor == "q"){
        returnMatchedValue = 1;
    }
        else if (lookFor == "w"){
        returnMatchedValue = 2;
    }
        else if (lookFor == "e"){
        returnMatchedValue = 3;
    }
        else if (lookFor == "r"){
        returnMatchedValue = 4;
    }
        else if (lookFor == "t"){
        returnMatchedValue = 5;
    }
    else if (lookFor == "y"){
        returnMatchedValue = 6;
    }
    else if (lookFor == "u"){
        returnMatchedValue = 7;
    }
    else if (lookFor == "i"){
        returnMatchedValue = 8;
    }
    else if (lookFor == "o"){
        returnMatchedValue = 9;
    }
    else if (lookFor == "p"){
        returnMatchedValue = 10;
    }
    else {
        returnMatchedValue = 0;
    }

        return returnMatchedValue;
}

var totalMatchedValue = 0;
totalMatchedValue += testThis("q");
totalMatchedValue += testThis("w");
totalMatchedValue += testThis("e");
totalMatchedValue += testThis("r");
totalMatchedValue += testThis("t");
totalMatchedValue += testThis("y");
totalMatchedValue += testThis("u");
totalMatchedValue += testThis("i");
totalMatchedValue += testThis("o");
totalMatchedValue += testThis("p");
pending…
CASE Statement
function testThis(lookFor) {
        var returnMatchedValue;
   
    switch (lookFor){
                case "q":
                        returnMatchedValue = 1;
                        break;
                case "w":
                        returnMatchedValue = 2;
                        break;
                case "e":
                        returnMatchedValue = 3;
                        break;
                case "r":
                        returnMatchedValue = 4;
                        break;
                case "t":
                        returnMatchedValue = 5;
                        break;
        case "y":
                        returnMatchedValue = 6;
                        break;
        case "u":
                        returnMatchedValue = 7;
                        break;
        case "i":
                        returnMatchedValue = 8;
                        break;
        case "o":
                        returnMatchedValue = 9;
                        break;
        case "p":
                        returnMatchedValue = 10;
                        break;
                default:
                        returnMatchedValue = 0;
                        break;
        }                      
        return returnMatchedValue;             
}

var totalMatchedValue = 0;
totalMatchedValue += testThis("q");
totalMatchedValue += testThis("w");
totalMatchedValue += testThis("e");
totalMatchedValue += testThis("r");
totalMatchedValue += testThis("t");
totalMatchedValue += testThis("y");
totalMatchedValue += testThis("u");
totalMatchedValue += testThis("i");
totalMatchedValue += testThis("o");
totalMatchedValue += testThis("p");
pending…

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

Compare results of other browsers

0 comments

Add a comment