Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object, If, Ternary or Switch
(version: 0)
Comparing performance of:
Object vs IFs vs Ternaries vs Switch
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Object
function getId (id) { var ocs = { '210-axhj': 'bestSeller', '210-ayze': 'bestSellingUltraSharp', '210-bfrp': 'bestSellingAlienware', '210-azzr': 'bestSellingGaming', '210-axkp': 'bestSellingSSeries', '210-bemm': 'bestSellingESeries', '210-bbck': 'bestSellingPSeries', '210-bejf': 'topRated', '210-bdpf': 'topRatedUltraSharp', '210-axvg': 'topRatedAlienware', '210-azep': 'topRatedGaming', '210-azzp': 'topRatedSSeries', '210-bbbk': 'topRatedESeries', '210-bbcc': 'topRatedPSeries', '210-bhym': 'newRelease', '210-bhbl': 'newRelease', '210-bhxc': 'newRelease', '210-bhsm': 'newRelease', '210-bhyc': 'newRelease' }; return ocs[id] || 'none'; } getId('210-bhyc');
IFs
function getId (id) { if (id === '210-axhj') { return 'bestSeller'; } else if (id === '210-ayze') { return 'bestSellingUltraSharp'; } else if (id === '210-bfrp') { return 'bestSellingAlienware'; } else if (id === '210-azzr') { return 'bestSellingGaming'; } else if (id === '210-axkp') { return 'bestSellingSSeries'; } else if (id === '210-bemm') { return 'bestSellingESeries'; } else if (id === '210-bbck') { return 'bestSellingPSeries'; } else if (id === '210-bejf') { return 'topRated'; } else if (id === '210-bdpf') { return 'topRatedUltraSharp'; } else if (id === '210-axvg') { return 'topRatedAlienware'; } else if (id === '210-azep') { return 'topRatedGaming'; } else if (id === '210-azzp') { return 'topRatedSSeries'; } else if (id === '210-bbbk') { return 'topRatedESeries'; } else if (id === '210-bbcc') { return 'topRatedPSeries'; } else if (id === '210-bhym' || id === '210-bhbl' || id === '210-bhxc' || id === '210-bhsm' || id === '210-bhyc') { return 'newRelease'; } return 'none'; } getId('210-bhyc');
Ternaries
function getId (id) { return id === '210-axhj' ? 'bestSeller' : id === '210-ayze' ? 'bestSellingUltraSharp' : id === '210-bfrp' ? 'bestSellingAlienware' : id === '210-azzr' ? 'bestSellingGaming' : id === '210-axkp' ? 'bestSellingSSeries' : id === '210-bemm' ? 'bestSellingESeries' : id === '210-bbck' ? 'bestSellingPSeries' : id === '210-bejf' ? 'topRated' : id === '210-bdpf' ? 'topRatedUltraSharp' : id === '210-axvg' ? 'topRatedAlienware' : id === '210-azep' ? 'topRatedGaming' : id === '210-azzp' ? 'topRatedSSeries' : id === '210-bbbk' ? 'topRatedESeries' : id === '210-bbcc' ? 'topRatedPSeries' : id === '210-bhym' || id === '210-bhbl' || id === '210-bhxc' || id === '210-bhsm' || id === '210-bhyc' ? 'newRelease' : 'none'; } getId('210-bhyc');
Switch
function getId (id) { switch (id) { case '210-axhj': return 'bestSeller'; case '210-ayze': return 'bestSellingUltraSharp'; case '210-bfrp': return 'bestSellingAlienware'; case '210-azzr': return 'bestSellingGaming'; case '210-axkp': return 'bestSellingSSeries'; case '210-bemm': return 'bestSellingESeries'; case '210-bbck': return 'bestSellingPSeries'; case '210-bejf': return 'topRated'; case '210-bdpf': return 'topRatedUltraSharp'; case '210-axvg': return 'topRatedAlienware'; case '210-azep': return 'topRatedGaming'; case '210-azzp': return 'topRatedSSeries'; case '210-bbbk': return 'topRatedESeries'; case '210-bbcc': return 'topRatedPSeries'; case '210-bhym': case '210-bhbl': case '210-bhxc': case '210-bhsm': case '210-bhyc': return 'newRelease'; default: return 'none'; } } getId('210-bhyc');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Object
IFs
Ternaries
Switch
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
To answer the question, I will provide an analysis of the benchmark results. The two top-performing tests are: 1. **Switch**: With 109,788,666 executions per second 2. **Ternaries**: With 108,001,370 executions per second Both tests have high execution rates, but Switch performs slightly better than Ternaries. To determine the reason for this difference, let's analyze the code: **Object test** The code is a simple object literal with several properties: ```javascript const obj = { foo: 'bar', baz: 42, qux: null, }; ``` This is likely to be executed multiple times in the benchmark, resulting in high execution counts. **Switch test** The code uses a switch statement with many cases: ```javascript function getId(id) { switch (id) { // ... default: return 'none'; } } ``` While this code may appear complex, the switch statement is actually a relatively simple data-driven approach. The high execution counts likely come from the large number of cases in the switch statement. **Ternaries test** The code uses a combination of ternary operators and simple conditional statements: ```javascript const result = id === '210-axhj' ? 'bestSeller' : ... ``` While this code is more complex than the object test, it's still relatively straightforward. However, the high number of executions suggests that the benchmark may be executing this code many times. **IFs test** The code uses several IF statements: ```javascript if (condition) { // ... } ``` This code is likely to be executed less frequently than the other tests, resulting in lower execution counts. Overall, the performance results suggest that: * Switch statements can perform well with a large number of cases. * Ternary operators and simple conditional statements can also execute quickly. * Object literals and IF statements tend to have lower execution rates due to their simplicity and lack of branching. Keep in mind that these are general observations based on the specific benchmark code. The actual performance results may depend on many factors, including the specific use case, input data, and hardware configurations.
Related benchmarks:
Switch vs If else CNC
Object vs Switch..Case vs Map
map vs ifelse vs switch test
if-else vs switch v2
switch(true) vs if-else
Comments
Confirm delete:
Do you really want to delete benchmark?