Switch statement vs single if logic

JavaScript performance comparison

Test case created by Anton

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
function sizeToBytes(size, format) {

        "use strict";

        switch (format) {
        case 'MB':
                return size * 1048576;
        case 'GB':
                return size * 1073741824;
        case 'TB':
                return size * 1099511627776;
        default:
                return size * 1024;
        }

}

var test = sizeToBytes(1,'KB');
pending…
Single If Logic
function size(amount, format) {
    var sizes = {
        'MB': 1048576,
        'GB': 1073741824,
        'TB': 1099511627776
    }

    if(sizes[format]) {
        return amount * sizes[format];
    }

    return amount * 1024;
}

var test = size(1,'KB');
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