Switch vs object lookup (v2)

Revision 2 of this benchmark created on


Description

Big branching on string values using switch or object lookup

Setup

const keys = ['agb', 'agi', 'agj', 'agm', 'ahb', 'ahi', 'ahj', 'ahm', 'atb', 'ati', 'atj', 'atm', 'axb', 'axi', 'axj', 'axm', 'fgb', 'fgi', 'fgj', 'fgm', 'fhb', 'fhi', 'fhj', 'fhm', 'ftb', 'fti', 'ftj', 'ftm', 'fxb', 'fxi', 'fxj', 'fxm', 'mgb', 'mgi', 'mgj', 'mgm', 'mhb', 'mhi', 'mhj', 'mhm', 'mtb', 'mti', 'mtj', 'mtm', 'mxb', 'mxi', 'mxj', 'mxm', 'tgb', 'tgi', 'tgj', 'tgm', 'thb', 'thi', 'thj', 'thm', 'ttb', 'tti', 'ttj', 'ttm', 'txb', 'txi', 'txj', 'txm', 'xxx'];
function runWithSwitch(key) {
    switch (key) {
        case 'agb': return 224250;
        case 'agi': return 480189;
        case 'agj': return 615054;
        case 'agm': return 964110;
        case 'ahb': return 379048;
        case 'ahi': return 118729;
        case 'ahj': return 499083;
        case 'ahm': return 295370;
        case 'atb': return 417127;
        case 'ati': return 307290;
        case 'atj': return 291255;
        case 'atm': return 29732;
        case 'axb': return 563482;
        case 'axi': return 64168;
        case 'axj': return 403358;
        case 'axm': return 331689;
        case 'fgb': return 870996;
        case 'fgi': return 241458;
        case 'fgj': return 615119;
        case 'fgm': return 222771;
        case 'fhb': return 898129;
        case 'fhi': return 369524;
        case 'fhj': return 228697;
        case 'fhm': return 266049;
        case 'ftb': return 868375;
        case 'fti': return 931981;
        case 'ftj': return 240061;
        case 'ftm': return 18130;
        case 'fxb': return 722916;
        case 'fxi': return 252100;
        case 'fxj': return 865328;
        case 'fxm': return 27946;
        case 'mgb': return 955507;
        case 'mgi': return 256735;
        case 'mgj': return 751968;
        case 'mgm': return 564802;
        case 'mhb': return 175261;
        case 'mhi': return 801536;
        case 'mhj': return 932507;
        case 'mhm': return 927466;
        case 'mtb': return 317865;
        case 'mti': return 447089;
        case 'mtj': return 821203;
        case 'mtm': return 710167;
        case 'mxb': return 216529;
        case 'mxi': return 101492;
        case 'mxj': return 188696;
        case 'mxm': return 203702;
        case 'tgb': return 815993;
        case 'tgi': return 192610;
        case 'tgj': return 154180;
        case 'tgm': return 999495;
        case 'thb': return 445672;
        case 'thi': return 853681;
        case 'thj': return 357307;
        case 'thm': return 916684;
        case 'ttb': return 288744;
        case 'tti': return 102020;
        case 'ttj': return 469320;
        case 'ttm': return 266505;
        case 'txb': return 636695;
        case 'txi': return 334826;
        case 'txj': return 741742;
        case 'txm': return 542771;
        default: return -1;
    }
}

const DATA = {
    "agb": 224250,
    "agi": 480189,
    "agj": 615054,
    "agm": 964110,
    "ahb": 379048,
    "ahi": 118729,
    "ahj": 499083,
    "ahm": 295370,
    "atb": 417127,
    "ati": 307290,
    "atj": 291255,
    "atm": 29732,
    "axb": 563482,
    "axi": 64168,
    "axj": 403358,
    "axm": 331689,
    "fgb": 870996,
    "fgi": 241458,
    "fgj": 615119,
    "fgm": 222771,
    "fhb": 898129,
    "fhi": 369524,
    "fhj": 228697,
    "fhm": 266049,
    "ftb": 868375,
    "fti": 931981,
    "ftj": 240061,
    "ftm": 18130,
    "fxb": 722916,
    "fxi": 252100,
    "fxj": 865328,
    "fxm": 27946,
    "mgb": 955507,
    "mgi": 256735,
    "mgj": 751968,
    "mgm": 564802,
    "mhb": 175261,
    "mhi": 801536,
    "mhj": 932507,
    "mhm": 927466,
    "mtb": 317865,
    "mti": 447089,
    "mtj": 821203,
    "mtm": 710167,
    "mxb": 216529,
    "mxi": 101492,
    "mxj": 188696,
    "mxm": 203702,
    "tgb": 815993,
    "tgi": 192610,
    "tgj": 154180,
    "tgm": 999495,
    "thb": 445672,
    "thi": 853681,
    "thj": 357307,
    "thm": 916684,
    "ttb": 288744,
    "tti": 102020,
    "ttj": 469320,
    "ttm": 266505,
    "txb": 636695,
    "txi": 334826,
    "txj": 741742,
    "txm": 542771
};

function runWithLookup(key) {
    return DATA[key] || -1;
}

const seq = Array.from({length: 200}, () => keys[Math.floor(Math.random() * keys.length)]);


Test runner

Ready to run.

Testing in
TestOps/sec
Switch
seq.map(runWithSwitch)
ready
Lookup
seq.map(runWithLookup)
ready

Revisions

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