if vs object (v5)

Revision 5 of this benchmark created on


Setup

// if-else
function getOrderMessage1(status) {
    if (status === 'pending') {
        return 'Order is pending.';
    } else if (status === 'shipped') {
        return 'Order has been shipped.';
    } else if (status === 'delivered') {
        return 'Order has been delivered.';
    }
}

// object with string keys
const orderMessages2 = {
    'pending': 'Order is pending.',
    'shipped': 'Order has been shipped.',
	'delivered': 'Order has been delivered.',
};

function getOrderMessage2(status) {
    return orderMessages2[status];
}

// object with number keys (similar to enum)
const orderMessages3 = {
    0: 'Order is pending.',
    1: 'Order has been shipped.',
	2: 'Order has been delivered.',
};

function getOrderMessage3(status) {
    return orderMessages3[status];
}

// switch-case
function getOrderMessage4(status) {
    switch (status) {
        case 'pending':
            return 'Order is pending.';
        case 'shipped':
            return 'Order has been shipped.';
        case 'delivered':
            return 'Order has been delivered.';
        default:
            return 'Unknown order status.';
    }
}

Test runner

Ready to run.

Testing in
TestOps/sec
if
getOrderMessage1('pending');
getOrderMessage1('shipped');
getOrderMessage1('delivered');
ready
object with string keys
getOrderMessage2('pending');
getOrderMessage2('shipped');
getOrderMessage2('delivered');
ready
object with numbered keys
getOrderMessage3(0);
getOrderMessage3(1);
getOrderMessage3(2);
ready
switch-case
getOrderMessage4('pending');
getOrderMessage4('shipped');
getOrderMessage4('delivered');
ready

Revisions

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