IF statement vs CASE Statement

Benchmark created by newbreedofgeek on


Description

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

Ready to run.

Testing in
TestOps/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");
ready
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");
ready

Revisions

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

  • Revision 1: published by newbreedofgeek on