Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch vs If else CNC
(version: 0)
Comparing performance of:
If else statement vs Switch statement
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function returnName(name) { if (name === '1Y') { return 'oneYearRor'; } if (name === '3Y') { return 'threeYearRor'; } if (name === '5Y') { return 'fiveYearRor'; } if (name === '10Y') { return 'tenYearRor'; } if (name === 'SI') { return 'sinceInceptionRor'; } if (name === 'YTD') { return null; } }; function switchStatement(name) { switch(name) { case '1Y': return 'oneYearRor'; case '3Y': return 'threeYearRor'; case '5Y': return 'fiveYearRor'; case '10Y': return 'tenYearRor'; case 'SI': return 'sinceInceptionRor'; default: return null; } }
Tests:
If else statement
returnName('1Y') returnName('3Y') returnName('5Y') returnName('10Y') returnName('SI') returnName('YTD')
Switch statement
switchStatement('1Y') switchStatement('3Y') switchStatement('5Y') switchStatement('10Y') switchStatement('SI') switchStatement('YTD')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
If else statement
Switch statement
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
If else statement
249795968.0 Ops/sec
Switch statement
241136608.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is tested?** The provided JSON represents two JavaScript microbenchmarks: "If else statement" and "Switch statement". The benchmarks compare the performance of two common control structures in JavaScript: if-else statements and switch statements. **Options compared** In this benchmark, the following options are compared: 1. If-else statements 2. Switch statements The benchmark tests how efficient each control structure is at determining the return value based on a set of predefined conditions. **Pros and Cons** Here's a brief analysis of the pros and cons of each option: * **If-else statements** + Pros: Generally easier to read and maintain, more flexible for complex logic. + Cons: Can be slower than switch statements due to the overhead of evaluating multiple conditions. * **Switch statements** + Pros: Typically faster than if-else statements since it only evaluates each case once. + Cons: More verbose and harder to read, especially for large numbers of cases. **Library/Function** The provided benchmark uses a custom function `returnName` that takes a string argument and returns a specific value based on the input. This function is used to create both if-else and switch statement benchmarks. **Special JS feature/syntax (None)** There are no special JavaScript features or syntaxes being tested in this benchmark. **Benchmark Preparation Code** The provided JSON contains two functions: `returnName` for creating the if-else benchmark, and `switchStatement` for creating the switch statement benchmark. These functions take a string argument as input and return a specific value based on the input. **Other alternatives** In general, if you need to compare control structures, you can also consider using other alternatives like: 1. **Template literals**: Which allow you to write template strings that can be used with switch statements. 2. **Object-based comparison**: Using objects to represent different cases and comparing them using `===` or `==`. 3. **Regular expressions**: If the logic is more complex and involves patterns. However, these alternatives may not provide a direct comparison between if-else and switch statements like the provided benchmark does. In conclusion, this benchmark provides a simple yet effective way to compare the performance of two common control structures in JavaScript: if-else statements and switch statements.
Related benchmarks:
JS switch vs if/else if
map vs ifelse vs switch test
switch case vs jump table vs bounce pattern vs ternary
Switch vs Functional Approach 3
Comments
Confirm delete:
Do you really want to delete benchmark?