Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
switch vs if else vs do while vs object
(version: 0)
Comparing performance of:
switch vs if else vs do while 0 vs object
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = 'yhahaha';
Tests:
switch
var b; switch(a) { case 'ahaha': b = 1; break; case 'ohoho': b = 2; break; case 'ihihi': b = 3; break; case 'yhahaha': b = 4; break; }
if else
var b; if (a === 'ahaha') { b = 1; } else if (a === 'ohoho') { b = 2; } else if (a === 'ihihi') { b = 3; } else if (a === 'yhahaha') { b = 4; }
do while 0
var b; do { if (a === 'ahaha') { b = 1; break; } if (a === 'ohoho') { b = 2; break; } if (a === 'ihihi') { b = 3; break; } if (a === 'yhahaha') { b = 4; } } while(0)
object
var b = { ahaha: 1, ohoho: 2, ihihi: 3, yhahaha: 4, }[a]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
switch
if else
do while 0
object
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
switch
16338544.0 Ops/sec
if else
4373649.5 Ops/sec
do while 0
4370581.5 Ops/sec
object
16349788.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark definition and test cases to explain what's being tested, compare different approaches, and discuss pros and cons. **Benchmark Definition** The benchmark measures the performance of four different approaches: 1. `switch` statement 2. `if-else` chain 3. `do-while` loop with nested `if`s 4. Object-based indexing Each approach is designed to achieve the same result: assigning a value to a variable `b` based on the string `a`. **Individual Test Cases** Here's a brief explanation of each test case: 1. **Switch Statement** ```javascript switch(a) { case 'ahaha': b = 1; break; case 'ohoho': b = 2; break; case 'ihihi': b = 3; break; case 'yhahaha': b = 4; break; } ``` The switch statement uses a `switch` keyword to evaluate the value of `a`. If the value matches one of the specified cases, the corresponding code is executed. 2. **If-Else Chain** ```javascript if (a === 'ahaha') { b = 1; } else if (a === 'ohoho') { b = 2; } else if (a === 'ihihi') { b = 3; } else if (a === 'yhahaha') { b = 4; } ``` This code uses an `if-else` chain to evaluate the value of `a`. Each conditional statement is checked in sequence until a match is found. 3. **Do-While Loop with Nested Ifs** ```javascript do { if (a === 'ahaha') { b = 1; break; } if (a === 'ohoho') { b = 2; break; } if (a === 'ihihi') { b = 3; break; } } while(0) ``` This code uses a `do-while` loop with nested `if` statements to evaluate the value of `a`. The loop continues until all conditions are met. 4. **Object-Based Indexing** ```javascript var b = { ahaha: 1, ohoho: 2, ihihi: 3, yhahaha: 4 }[a] ``` This code uses an object with string keys to access a value based on the value of `a`. **Pros and Cons** Here's a brief analysis of each approach: * **Switch Statement**: Pros - concise, efficient for multiple cases. Cons - may not be suitable for more complex logic. * **If-Else Chain**: Pros - easy to read, maintainable. Cons - can become cluttered for many conditions. * **Do-While Loop with Nested Ifs**: Pros - can be optimized for performance. Cons - can be challenging to read and maintain. * **Object-Based Indexing**: Pros - concise, flexible. Cons - may not be suitable for all data structures. **Library Usage** The benchmark uses the `break` statement, which is a built-in JavaScript statement that exits the switch block. **Special JS Features** There are no special JavaScript features or syntax used in this benchmark. **Alternative Approaches** Other alternatives to these approaches include: * **Array Indexing**: Instead of using an object, you can use an array with string keys. * **Regular Expressions**: You can use regular expressions to match the value of `a` and assign a value to `b`. * **Polymorphic Functions**: You can define a function that takes an argument and returns a value based on the input. Keep in mind that these alternatives may have performance implications or trade-offs in terms of maintainability and readability.
Related benchmarks:
switch vs if-else vs lookup
Switch vs Object Literal Performance Check case 40
JS switch vs if/else if
Switch vs Object Literal vs If Else
Comments
Confirm delete:
Do you really want to delete benchmark?