Last active
August 29, 2015 14:04
-
-
Save davestewart/a43df99f42ea157654ee to your computer and use it in GitHub Desktop.
Comparison of ternary, if/else, switch and hashed-methods to calculate a value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Comparison of ternary, if/else, switch and hashed-methods to calculate a value | |
* | |
* @author dave stewart | |
* @date weds 16th january 2014 | |
* @see conversation at http://codepen.io/markmiro/blog/awesome-ternary-operator | |
* | |
* Results of 100,000 tests each: | |
* | |
* - testIf : 35 ms = 1.52 x | |
* - testTernary : 35 ms = 1.52 x | |
* - testSwitchEarly : 23 ms = 1.00 x | |
* - testSwitchLate : 23 ms = 1.00 x | |
* - testObj : 483 ms = 21.00 x | |
* - testLazyObjInt : 471 ms = 20.48 x | |
* - testLazyObjExt : 114 ms = 4.96 x | |
*/ | |
// -------------------------------------------------------------------------------- | |
// tests | |
/* 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; } | |
}; | |
/* null test to set up environment */ | |
function testNull() | |
{ | |
return; | |
} | |
// -------------------------------------------------------------------------------- | |
// test runner | |
// tests | |
var tests = | |
{ | |
'testNull' : testNull, | |
'testIf' : testIf, | |
'testTernary' : testTernary, | |
'testSwitchEarly' : testSwitchEarly, | |
'testSwitchLate' : testSwitchLate, | |
'testObj' : testObj, | |
'testLazyObjInt' : testLazyObjInt, | |
'testLazyObjExt' : testLazyObjExt | |
}; | |
// test variables | |
var ops = ['==', '^=', '$=', '~=','!=']; | |
var numTests = 100000; | |
// variables | |
var name, | |
i, | |
j, | |
test, | |
start, | |
value, | |
result, | |
fastest = Number.MAX_VALUE, | |
results = {}; | |
// run tests | |
for (name in tests) | |
{ | |
// variables | |
test = tests[name]; | |
// debug | |
console.log('running test: ' + name); | |
// run tests | |
start = Date.now(); | |
for (i = 0; i < numTests; i++) | |
{ | |
for (j = 0; j < ops.length; j++) | |
{ | |
value = test(ops[j], 'some value'); | |
} | |
} | |
// end | |
if(test !== testNull) | |
{ | |
results[name] = result = Date.now() - start; | |
fastest = result < fastest ? result : fastest; | |
} | |
} | |
// results | |
console.clear(); | |
for (name in results) | |
{ | |
console.log(name, ':', results[name] + ' ms =', (results[name] / fastest).toFixed(2) + ' x'); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment