Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
if-else vs switch-case vs object literals vs ternary-operator
(version: 0)
Comparing performance of:
If - Else vs Switch - Case vs Object literals vs ternary-operator
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = 'abc'; str = str.charAt(Math.floor(Math.random() * 3));
Tests:
If - Else
if(str === 'a'){ console.log('A'); } else if (str === 'b'){ console.log('B'); } else if (str === 'c'){ console.log('C'); }
Switch - Case
switch (str) { case 'a': console.log('A'); break; case 'b': console.log('B'); break; case 'c': console.log('C'); break; }
Object literals
var objLiteral = { a: function() { console.log('A'); }, b: function() { console.log('B'); }, c: function() { console.log('C'); } } objLiteral[str]();
ternary-operator
str === 'a' ? console.log('A') : str === 'b' ? console.log('B') : str === 'c' ? console.log('C') : console.log('nothing')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
If - Else
Switch - Case
Object literals
ternary-operator
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
If - Else
13398.5 Ops/sec
Switch - Case
14807.4 Ops/sec
Object literals
11138.7 Ops/sec
ternary-operator
13226.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark measures the performance of different JavaScript control flow structures: 1. If-Else statements 2. Switch-Case statements 3. Object literals (using function invocation) 4. Ternary operators The goal is to determine which approach is the fastest for a specific use case, in this case, setting a variable `str` to one of three possible values: 'a', 'b', or 'c', and then performing operations on that value. **Test Cases** Each test case represents a single control flow structure. Here's a brief explanation of each: 1. **If-Else**: This test case uses an if-else statement with three conditions, checking for 'a', 'b', and 'c'. If the condition matches, it logs a message to the console. 2. **Switch-Case**: Similar to the if-else test, but using a switch-case statement instead. The values are still 'a', 'b', or 'c'. 3. **Object literals**: This test case uses an object literal with three functions (one for each possible value of `str`). It then invokes one of these functions based on the current value of `str`. 4. **Ternary Operator**: A single-line ternary operator is used to set a variable to 'A', 'B', or 'C' based on the value of `str`. Then, it logs a message to the console. **Pros and Cons** Each approach has its trade-offs: 1. **If-Else statements**: * Pros: Simple to read and write, flexible. * Cons: Can be slower due to branch prediction issues and the need for multiple checks. 2. **Switch-Case statements**: * Pros: Fast lookup times, suitable for discrete values. * Cons: Limited flexibility if value ranges are not well-defined. 3. **Object literals**: * Pros: Can encapsulate complex logic, flexible data structures. * Cons: May be slower due to function invocation and object lookups. 4. **Ternary Operator**: * Pros: Concise and readable, suitable for simple conditional expressions. * Cons: Limited flexibility if the condition is not a simple equality check. **Browser-Specific Notes** The benchmark results show that Chrome 129 on Mac OS X 10.15.7 favors switch-case statements ( highest `ExecutionsPerSecond` value). This might be due to the way switch-case statements are optimized in modern JavaScript engines. **Other Considerations** * **Function Invocation**: The object literal test case involves function invocation, which can add overhead. * **Branch Prediction**: If-Else and Switch-Case statements rely on branch prediction to execute faster. Poor branch prediction or misaligned code execution can negatively impact performance. Keep in mind that these results are specific to the provided benchmarking scenario and may not generalize to other use cases or JavaScript engines. As for alternatives, you could consider: * Using `Object.hasOwn()` instead of property lookups * Implementing custom loops for each control flow structure (e.g., a switch-case loop) * Optimizing the logic using bitwise operators or other optimizations specific to the target architecture.
Related benchmarks:
simple Math.max vs ternary
Math.max/min vs if vs ternary operator #2
Math.max/min vs if vs ternary operator 232323
Math.max/min vs if vs ternary operatorsd
ParseInt vs conditional ~~
Comments
Confirm delete:
Do you really want to delete benchmark?