Comparison of ternary, if/else, switch and hashed-methods to calculate a value

Benchmark created by Dave Stewart on


Setup

/* nested ternary */
    function testTernary(operand, value)
    {
        var rxStr   = operand === '=='
                                        ? '^' + value + '$'
                                : operand === '^='
                                        ? '^' + value
                                : operand === '$='
                                        ? value + '$'
                                : operand === '~='
                                        ? '\\b' + value + '\\b'
                                : operand === '*='
                                        ? value
                                : null;
        return rxStr;
    }
    
    /* if/else */
    function testIf(operand, value)
    {
        var rxStr;
        if (operand === '==')       rxStr = '^' + value + '$';
        else if (operand === '^=')  rxStr = '^' + value;
        else if (operand === '$=')  rxStr = value + '$';
        else if (operand === '~=')  rxStr = '\\b' + value + '\\b';
        else if (operand === '*=')  rxStr = value;
        return rxStr;
    }
    
    /* switch with early-exit */
    function testSwitchEarly(operand, value)
    {
        switch(value)
        {
                case '=='       : return '^' + value + '$';
                case '^='       : return '^' + value;
                case '$='       : return value + '$';
                case '~='       : return '\\b' + value + '\\b'; 
                case '*='       : return value;
        }
        return;
    }
    
    /* switch with early-exit */
    function testSwitchLate(operand, value)
    {
        var rxStr;
        switch(value)
        {
                case '==':
                        rxStr = '^' + value + '$';
                        break;
                case '^=':
                        rxStr = '^' + value; 
                        break;
                case '$=':
                        rxStr = value + '$'; 
                        break;
                case '~=':
                        rxStr = '\\b' + value + '\\b'; 
                        break;
                case '*=':
                        rxStr = value; 
                        break;
        }
        return rxStr;
    }
    
    /* object with values calculated each time */
    function testObj(operand, value)
    {
        var operands =
        {
                '=='            : '^' + value + '$',
                '$='            : value + '$',
                '^='            : '^' + value,
                '~='            : '\\b' + value + '\\b',
                '*='            : value
        };
        return operands[operand];
    }
    
    /* object with values calculated by a function, declared each time */
    function testLazyObjInt(operand, value)
    {
        var operands =
        {
                '=='            : function(){ return '^' + value + '$'; },
                '^='            : function(){ return '^' + value; },
                '$='            : function(){ return value + '$'; },
                '~='            : function(){ return '\\b' + value + '\\b'; },
                '*='            : function(){ return value; }
        };
        if(operand in operands)
        {
                return operands[operand]();
        }
        return;
    }
    
    /* object with values calculated by a function, declared once */
    function testLazyObjExt(operand, value)
    {
        if(operand in operands)
        {
                return operands[operand](value);
        }
        return;
    }
    
    var operands =
    {
        '=='            : function(value){ return '^' + value + '$'; },
        '^='            : function(value){ return '^' + value; },
        '$='            : function(value){ return value + '$'; },
        '~='            : function(value){ return '\\b' + value + '\\b'; },
        '*='            : function(value){ return value; }
    };
    
    var ops         = ['==', '^=', '$=', '~=','!='];

Test runner

Ready to run.

Testing in
TestOps/sec
ternary
for (var i = 0; i < ops.length; i++)
{
        value = testTernary(ops[i], 'some value');
}
 
ready
if
for (var i = 0; i < ops.length; i++)
{
        value = testIf(ops[i], 'some value');
}
ready
switch (early exit)
for (var i = 0; i < ops.length; i++)
{
        value = testSwitchEarly(ops[i], 'some value');
}
 
ready
switch (late exit)
for (var i = 0; i < ops.length; i++)
{
        value = testSwitchLate(ops[i], 'some value');
}
ready
calculate all values
for (var i = 0; i < ops.length; i++)
{
        value = testObj(ops[i], 'some value');
}
ready
get value from function (internal)
for (var i = 0; i < ops.length; i++)
{
        value = testLazyObjInt(ops[i], 'some value');
}
ready
get value from function (global)
for (var i = 0; i < ops.length; i++)
{
        value = testLazyObjExt(ops[i], 'some value');
}
ready

Revisions

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

  • Revision 1: published by Dave Stewart on