Switch vs Repeating if

JavaScript performance comparison

Test case created by Josh

Info

Comparing switch vs multiple if statements.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var contentType = "json", cT;
};
</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
Switch
switch (contentType) {
                case "json":
                        cT = 'application/json';
                        break;
                case "txt":
                        cT = 'application/txt';
                        break;
                case "mp3":
                        cT = 'application/mp3';
                        break;
    case "mp4":
                        cT = 'application/mp3';
                        break;
    case "test":
                        cT = 'application/mp3';
                        break;
    case "test1":
                        cT = 'application/mp3';
                        break;
                default:
                        cT = 'application/json'
                        break;
        }
pending…
If
if(contentType == "json") cT = 'application/json';
else if(contentType == "txt") cT = 'application/txt';
else if(contentType == "mp3") cT = 'application/mp3';
else if(contentType == "mp4") cT = 'application/mp3';
else if(contentType == "test") cT = 'application/mp3';
else if(contentType == "test1") cT = 'application/mp3';
else cT = 'application/json';
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