if vs object (v2)

Revision 2 of this benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
if
function getOrderMessage(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.';
    }
}

getOrderMessage('pending');
getOrderMessage('shipped');
getOrderMessage('delivered');
ready
object
const orderMessages = {
    'pending': 'Order is pending.',
    'shipped': 'Order has been shipped.',
	'delivered': 'Order has been delivered.',
};

function getOrderMessage(status) {
    return orderMessages[status];
}

getOrderMessage('pending');
getOrderMessage('shipped');
getOrderMessage('delivered');
ready
object with numbered keys
const orderMessages = {
    0: 'Order is pending.',
    1: 'Order has been shipped.',
	2: 'Order has been delivered.',
};

function getOrderMessage(status) {
    return orderMessages[status];
}

getOrderMessage(0);
getOrderMessage(1);
getOrderMessage(2);
ready
switch-case
function getOrderMessage(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.';
    }
}

getOrderMessage('pending');
getOrderMessage('shipped');
getOrderMessage('delivered');
ready

Revisions

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