Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string switch vs includes
(version: 6)
Comparing performance of:
switch with includes vs if-else
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var testString = "abc.bcd.cde.def.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb"
Tests:
switch with includes
testString.split(".").forEach(str => { if(["abc","bcd","cde","def"].includes(str)){ switch(str){ case "abc": console.log(str); case "bcd": console.log(str); case "cde": console.log(str); case "def": console.log(str); default: } } });
if-else
testString.split(".").forEach(str => { if(str === "abc"){ console.log(str); } else if(str === "bcd"){ console.log(str); } else if(str === "cde"){ console.log(str); } else if(str === "def"){ console.log(str); } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
switch with includes
if-else
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 SberBrowser/21.0.0.0
Browser/OS:
Chrome 129 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
switch with includes
22443.1 Ops/sec
if-else
50784.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's dive into the explanation. The benchmark tests two different approaches to switch between multiple values in JavaScript: using an `if-else` statement or using a `switch` statement with an array of expected values and the `includes()` method. **Test Case 1: Switch with includes** This test case uses a `switch` statement with an array of expected values (`["abc", "bcd", "cde", "def"]`) and checks if each value is included in that array. If it is, the corresponding `console.log(str)` statement is executed. The code snippet: ```javascript var testString = "abc.bcd.cde.def.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb"; testString.split(".").forEach(str => { if(["abc","bcd","cde","def"].includes(str)){ switch(str){ case "abc": console.log(str); case "bcd": console.log(str); case "cde": console.log(str); case "def": console.log(str); default: } } }); ``` **Test Case 2: If-else** This test case uses an `if-else` statement chain to switch between multiple values. For each value, a separate `console.log(str)` statement is executed. The code snippet: ```javascript var testString = "abc.bcd.cde.def.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb.asdb"; testString.split(".").forEach(str => { if(str === "abc"){ console.log(str); } else if(str === "bcd"){ console.log(str); } else if(str === "cde"){ console.log(str); } else if(str === "def"){ console.log(str); } }); ``` **Comparison** The benchmark results show that the `if-else` approach is significantly slower (8876.55 executions per second) compared to the `switch with includes` approach (21,573.59 executions per second). This suggests that using a `switch` statement with an array of expected values can be a more efficient way to switch between multiple values in JavaScript. **Pros and Cons** * **Switch with includes:** + Pros: - More concise code - Faster execution speed (based on the benchmark results) + Cons: - May not work if the array of expected values is not exhaustive - Can be less readable than an `if-else` statement chain * **If-else:** + Pros: - More explicit and easier to read - Works even if the array of expected values is not exhaustive + Cons: - Longer code due to the need for multiple `if` statements - Slower execution speed (based on the benchmark results) **Other Considerations** * The choice between `switch with includes` and `if-else` depends on the specific use case and personal preference. If conciseness and performance are priorities, `switch with includes` might be a better choice. * In cases where the array of expected values is not exhaustive, an `if-else` statement chain or another approach should be used to ensure accurate switching behavior. No external libraries or special JS features are used in these test cases. The only JavaScript feature used is the `includes()` method, which checks if a value exists within an array.
Related benchmarks:
Switch vs Object Literal v2302302
Switch vs Object Literal v23023022323
Op vs localeCompare
Javascript: Case insensitive string comparison performance with localeCompare call updated to specify locale.
Comments
Confirm delete:
Do you really want to delete benchmark?