Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
switch vs if (3)
(version: 1)
Comparing performance of:
switch vs if
Created:
2 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script> var arr = []; var res = [0, 0]; var generateRandomInteger = function(min, max) { return Math.floor(min + Math.random() * (max - min + 1)) } var test01 = function() { let k = 0; for (const v of arr) { switch (v) { case 1: k += 1; break; case 2: k += 2; break; case 3: k += 3; break; case 4: k += 5; break; case 5: k += 7; break; case 6: k += 11; break; case 7: k += 13; break; case 8: k += 17; break; default: k += 19;; } } res[0] = k; } var test02 = function() { let k = 0; for (const v of arr) { if (v === 1) { k += 1; } else if (v === 2) { k += 2; } else if (v === 3) { k += 3; } else if (v === 4) { k += 5; } else if (v === 5) { k += 7; } else if (v === 6) { k += 11; } else if (v === 7) { k += 13; } else if (v === 8) { k += 17; } else { k += 19; } } res[1] = k; } </script>
Script Preparation code:
Benchmark.prototype.setup = function() { arr = new Array(256).fill(0).map(e => generateRandomInteger(1, 16)); if (test01() !== test02()) throw new Error('test error'); }; Benchmark.prototype.teardown = function() { arr.length = 0; res = [0, 0]; }
Tests:
switch
test01();
if
test02();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
switch
if
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Zaap3.6.8
Browser/OS:
Chrome 123 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
switch
1248273.2 Ops/sec
if
2987279.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark definition and explore what's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of two approaches: using a `switch` statement versus an `if-else` chain. The test data is generated by creating an array of 256 random integers between 1 and 16, which are then used as input values for both tests. **Script Preparation Code** The script preparation code sets up the benchmark environment. It defines two functions, `test01()` and `test02()`, which will be executed in the benchmark. These functions are responsible for calculating a value `k` based on the input array `arr`. The difference between these functions is that `test01()` uses a `switch` statement, while `test02()` uses an `if-else` chain. **Html Preparation Code** The HTML preparation code defines a script that generates a random integer function `generateRandomInteger()`, which will be used to populate the input array `arr`. It also initializes variables `arr`, `res`, and defines the two test functions `test01()` and `test02()`. **Individual Test Cases** There are two individual test cases: 1. `switch`: This test case executes the `test01()` function, which uses a `switch` statement to calculate the value `k`. 2. `if`: This test case executes the `test02()` function, which uses an `if-else` chain to calculate the value `k`. **Library and Purpose** There is no explicit library mentioned in the benchmark definition. However, it's possible that some internal libraries or frameworks are being used under the hood. **Special JS Feature/Syntax** The benchmark does not explicitly use any special JavaScript features or syntax. However, it does rely on ES6+ features like `const`, `let`, and template literals. **Performance Comparison** The benchmark is designed to compare the performance of two approaches: 1. **Switch Statement**: The `switch` statement is a built-in control structure in JavaScript that allows for efficient comparison and branch execution. 2. **If-Else Chain**: An `if-else` chain is a more general-purpose control structure that can be used for conditional execution, but may incur additional overhead due to repeated comparisons. **Pros and Cons** Here are some pros and cons of each approach: * **Switch Statement**: + Pros: - Fast branch prediction - Low overhead for comparison operations + Cons: - Limited flexibility in handling unknown cases - May require more code to handle multiple branches * **If-Else Chain**: + Pros: - Highly flexible and adaptable to new cases - Can be used for non-boolean comparisons (e.g., string matching) + Cons: - Higher overhead due to repeated comparison operations - May have slower branch prediction **Other Alternatives** Some other alternatives that could be considered for this benchmark include: 1. **Object-based approach**: Instead of using a `switch` statement or an `if-else` chain, the code could use object-based methods (e.g., `switch` on an object property) to compare values. 2. **Regex-based approach**: The code could use regular expressions (regex) to match and compare values. 3. **Native array method**: JavaScript arrays have native methods like `indexOf()` or `findIndex()` that can be used for searching and comparison. These alternatives may offer different performance characteristics, trade-offs in flexibility and readability, and require different optimizations or considerations.
Related benchmarks:
switch vs if vs object selector
For vs Min
For vs Min1
array vs float64 for io and slice
Comments
Confirm delete:
Do you really want to delete benchmark?