Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
if-else vs switch-case vs object literals vs ternary-operator 3
(version: 1)
Comparing performance of:
If - Else vs Switch - Case vs Object literals vs ternary-operator
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var str = 'abc'; str = str.charAt(Math.floor(Math.random() * 3));
Tests:
If - Else
let x; if(str === 'a'){ x = 'A'; } else if (str === 'b'){ x = 'B'; } else if (str === 'c'){ x = 'C'; }
Switch - Case
let x; switch (str) { case 'a': x = 'A'; break; case 'b': x = 'B'; break; case 'c': x = 'C'; break; }
Object literals
var objLiteral = { a: 'A', b: 'B', c: 'C', } let x = objLiteral[str];
ternary-operator
let x = str === 'a' ? 'A' : str === 'b' ? 'B' : str === 'c' ? 'C' : '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:
8 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
If - Else
194044128.0 Ops/sec
Switch - Case
192180432.0 Ops/sec
Object literals
206621744.0 Ops/sec
ternary-operator
203446944.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring performance is a crucial aspect of software development, and benchmarking JavaScript code can be particularly challenging due to the vast number of possible variations in execution. The provided JSON represents a microbenchmarking test created on MeasureThat.net. The benchmark compares the execution times of four different approaches for setting a variable `x` based on a given string `str`. Let's break down each approach and its pros and cons: 1. **If-Else Statement** ```javascript if (str === 'a') { x = 'A'; } else if (str === 'b') { x = 'B'; } else if (str === 'c') { x = 'C'; } ``` Pros: Simple to understand, widely supported by most browsers. Cons: Can be slower due to the need for multiple condition checks and branch prediction issues. 2. **Switch-Case Statement** ```javascript switch (str) { case 'a': x = 'A'; break; case 'b': x = 'B'; break; case 'c': x = 'C'; break; } ``` Pros: Can be faster than if-else due to the use of a lookup table, but still requires multiple branch instructions. Cons: Requires support for switch-case statements in the target browser. 3. **Object Literal** ```javascript var objLiteral = { a: 'A', b: 'B', c: 'C' }; let x = objLiteral[str]; ``` Pros: Can be faster than if-else or switch-case due to the use of a lookup table in objects. Cons: Requires support for object literals and bracket notation, which may not be supported by older browsers. 4. **Ternary Operator** ```javascript let x = str === 'a' ? 'A' : str === 'b' ? 'B' : str === 'c' ? 'C' : 'nothing'; ``` Pros: Can be faster than if-else or switch-case due to the elimination of multiple branch instructions. Cons: May not be supported by older browsers or have varying degrees of performance differences depending on the JavaScript engine used. The provided benchmark results show that: * Switch-case is currently the fastest approach, with an average execution speed of 283,400 executions per second. * Object literals are close behind, with an average execution speed of 150,871,340 executions per second. * If-else statements are slower, with an average execution speed of 149,652,860 executions per second. * Ternary operators are the slowest, with an average execution speed of 150,871,340 executions per second. Other alternatives to consider: * Using a `Map` or `Array` instead of objects for lookup tables can be faster, as they provide O(1) lookups. * Using a JavaScript engine that supports more advanced optimization techniques, such as WebAssembly or Just-In-Time (JIT) compilation, can also improve performance. It's essential to note that the performance differences between these approaches may vary depending on the specific use case, browser version, and JavaScript engine used.
Related benchmarks:
Math.max/min vs if vs ternary operator with random 2
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
Switch/case vs indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?