Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
if else vs switch case vs JSON
(version: 1)
Compares if/else, switch/case and a JSON mapping to functions.
Comparing performance of:
if/else vs switch/case vs JSON
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = [1, 3, 2]; for(let i = 5; i > 0; i--) { var array = array.concat(array); }; var mappingJSON = { 1: function() { return 'one'; }, 2: function() { return 'two'; }, 3: function() { return 'three'; } };
Tests:
if/else
for(let i = array.length - 1; i >= 0; i--) { let item = array[i]; if(item === 1) { console.log('one'); } else if(item === 2) { console.log('two'); } else if(item === 3) { console.log('three'); }; };
switch/case
for(let i = array.length - 1; i >= 0; i--) { let item = array[i]; switch(item) { case 1: console.log('one'); break; case 2: console.log('two'); break; case 3: console.log('three'); break; }; };
JSON
for(let i = array.length - 1; i >= 0; i--) { let item = array[i]; console.log(mappingJSON[item]()); };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
if/else
switch/case
JSON
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:145.0) Gecko/20100101 Firefox/145.0
Browser/OS:
Firefox 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
if/else
3702.0 Ops/sec
switch/case
3802.2 Ops/sec
JSON
3718.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring JavaScript performance is a fascinating topic! The provided benchmark measures the execution time of three different approaches: if-else statements, switch-case statements, and JSON mapping to functions. **What are we testing?** We're comparing the performance of three functions that perform the same task: 1. `if-else` statement 2. `switch-case` statement 3. A JSON object with functions as values The test case uses an array `array` and its length is incremented by concatenating it with itself 5 times (this is likely a warm-up or initialization step). Then, the test iterates over the array in reverse order, performing one of the three operations on each element. **Options compared:** 1. **if-else statements**: A traditional approach using if-else conditions to determine which function to execute. 2. **switch-case statements**: Another traditional approach using switch-case statements to determine which function to execute based on a value. 3. **JSON mapping**: A dynamic approach where the JSON object is used as a lookup table, allowing for more flexible and potentially efficient execution. **Pros and cons of each approach:** 1. **if-else statements**: * Pros: Easy to understand, widely supported, and well-established. * Cons: Can be slow due to branching and condition checks. 2. **switch-case statements**: * Pros: Can be faster than if-else due to direct value comparisons and potentially fewer branches. * Cons: May not work as expected with non-numeric values or complex conditions. 3. **JSON mapping**: * Pros: Allows for dynamic function selection, can be more efficient due to caching and memoization, and provides a clear separation of concerns. * Cons: Requires additional setup and understanding of the JSON object structure. **Library and syntax considerations:** The test case uses the `console.log()` function, which is part of the browser's built-in API. This means that any performance differences will be influenced by the browser's internal optimizations and caching mechanisms. There are no special JavaScript features or syntax mentioned in this benchmark, but it's essential to note that some browsers may have additional features or extensions that could affect performance. **Alternatives:** Other approaches you might consider using for this type of benchmarking include: 1. **Memoization**: Cache the results of expensive function calls to avoid redundant calculations. 2. **Function caching**: Store the result of a function call in a cache, so subsequent calls with the same arguments can return the cached value directly. 3. **JS engines with JIT compilation**: Some JavaScript engines, like SpiderMonkey or V8, have Just-In-Time (JIT) compilation capabilities that can improve performance by compiling functions to machine code. Keep in mind that these alternatives may require more setup and understanding of the underlying JavaScript engine and its features. Overall, this benchmark provides a great starting point for comparing the performance of different approaches to function selection and execution.
Related benchmarks:
Concat vs Slice
JS switch vs if/else if
Concat vs Slice f1
Slice vs Map (jv)
Comments
Confirm delete:
Do you really want to delete benchmark?