Switch vs Lookup inclusion test

Benchmark created on


Description

Check if the key is a known string

Setup

const keys = ['bgb', 'bgf', 'bgq', 'bgr', 'bmb', 'bmf', 'bmq', 'bmr', 'bpb', 'bpf', 'bpq', 'bpr', 'bqb', 'bqf', 'bqq', 'bqr', 'mgb', 'mgf', 'mgq', 'mgr', 'mmb', 'mmf', 'mmq', 'mmr', 'mpb', 'mpf', 'mpq', 'mpr', 'mqb', 'mqf', 'mqq', 'mqr', 'sgb', 'sgf', 'sgq', 'sgr', 'smb', 'smf', 'smq', 'smr', 'spb', 'spf', 'spq', 'spr', 'sqb', 'sqf', 'sqq', 'sqr', 'vgb', 'vgf', 'vgq', 'vgr', 'vmb', 'vmf', 'vmq', 'vmr', 'vpb', 'vpf', 'vpq', 'vpr', 'vqb', 'vqf', 'vqq', 'vqr'];
function runWithSwitch(key) {
    switch (key) {
        case 'bgb':
        case 'bgf':
        case 'bgq':
        case 'bgr':
        case 'bmb':
        case 'bmf':
        case 'bmq':
        case 'bmr':
        case 'bpb':
        case 'bpf':
        case 'bpq':
        case 'bpr':
        case 'bqb':
        case 'bqf':
        case 'bqq':
        case 'bqr':
        case 'mgb':
        case 'mgf':
        case 'mgq':
        case 'mgr':
        case 'mmb':
        case 'mmf':
        case 'mmq':
        case 'mmr':
        case 'mpb':
        case 'mpf':
        case 'mpq':
        case 'mpr':
        case 'mqb':
        case 'mqf':
        case 'mqq':
        case 'mqr':
        case 'sgb':
        case 'sgf':
        case 'sgq':
        case 'sgr':
        case 'smb':
        case 'smf':
        case 'smq':
        case 'smr':
        case 'spb':
        case 'spf':
        case 'spq':
        case 'spr':
        case 'sqb':
        case 'sqf':
        case 'sqq':
        case 'sqr':
        case 'vgb':
        case 'vgf':
        case 'vgq':
        case 'vgr':
        case 'vmb':
        case 'vmf':
        case 'vmq':
        case 'vmr':
        case 'vpb':
        case 'vpf':
        case 'vpq':
        case 'vpr':
        case 'vqb':
        case 'vqf':
        case 'vqq':
        case 'vqr': return true;
        default: return false;
    }
}
const DATA = {
    "bgb": true,
    "bgf": true,
    "bgq": true,
    "bgr": true,
    "bmb": true,
    "bmf": true,
    "bmq": true,
    "bmr": true,
    "bpb": true,
    "bpf": true,
    "bpq": true,
    "bpr": true,
    "bqb": true,
    "bqf": true,
    "bqq": true,
    "bqr": true,
    "mgb": true,
    "mgf": true,
    "mgq": true,
    "mgr": true,
    "mmb": true,
    "mmf": true,
    "mmq": true,
    "mmr": true,
    "mpb": true,
    "mpf": true,
    "mpq": true,
    "mpr": true,
    "mqb": true,
    "mqf": true,
    "mqq": true,
    "mqr": true,
    "sgb": true,
    "sgf": true,
    "sgq": true,
    "sgr": true,
    "smb": true,
    "smf": true,
    "smq": true,
    "smr": true,
    "spb": true,
    "spf": true,
    "spq": true,
    "spr": true,
    "sqb": true,
    "sqf": true,
    "sqq": true,
    "sqr": true,
    "vgb": true,
    "vgf": true,
    "vgq": true,
    "vgr": true,
    "vmb": true,
    "vmf": true,
    "vmq": true,
    "vmr": true,
    "vpb": true,
    "vpf": true,
    "vpq": true,
    "vpr": true,
    "vqb": true,
    "vqf": true,
    "vqq": true,
    "vqr": true
};
function runWithLookupValue(key) {
    return Boolean(DATA[key]);
}
function runWithLookupIn(key) {
    return key in DATA;
}

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

Test runner

Ready to run.

Testing in
TestOps/sec
Switch
seq.map(runWithSwitch);
ready
Lookup value
seq.map(runWithLookupValue);
ready
Lookup using "in"
seq.map(runWithLookupIn);
ready

Revisions

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