Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
searching_new2
(version: 0)
comparing if-else vs. switch vs. filter/includes vs. find/includes
Comparing performance of:
if-else vs switch vs amatch-filter vs amatch-find
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var item = 33; var data = [ { range: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], value: "less than 20", }, { range: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], value: "less than 30", }, { range: [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], value: "less than 40", }, { range: [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 25], value: "less than 50", }, ];
Tests:
if-else
if (item < 10) { console.log("less than 10"); } else if (item < 20) { console.log("less than 20"); } else if (item < 30) { console.log("less than 30"); } else if (item < 40) { console.log("less than 40"); } else if (item < 50) { console.log("less than 50"); } else { console.log("50 or more!"); }
switch
switch (item) { case item < 10: console.log("less than 10"); break; case item < 20: console.log("less than 20"); break; case item < 30: console.log("less than 30"); break; case item < 40: console.log("less than 40"); break; case item < 50: console.log("less than 50"); break; default: console.log("50 or more!"); }
amatch-filter
const amatch = () => { // Exclusions - borders - out-of-bound if (item < 10) return "less than 10"; if (item >= 50) return "50 or more!"; // within range return (data.filter(record => record.range.includes(item)))[0].value }; amatch(item);
amatch-find
const amatch = () => { // Exclusions - borders - out-of-bound if (item < 10) return "less than 10"; if (item >= 50) return "50 or more!"; // within range return data.find((record) => record.range.includes(item)).value; }; amatch(item);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
if-else
switch
amatch-filter
amatch-find
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of different approaches for finding an item within a range in an array. The test case includes four individual tests: 1. `if-else` 2. `switch` 3. `amatch-filter` (using `filter()` method) 4. `amatch-find` (using `find()` method) **What's being tested?** In each test, a variable `item` is set to a specific value (33), and an array `data` with multiple records is defined. The goal is to find the record in `data` that corresponds to the range of `item`. The tests compare the performance of different approaches: * `if-else`: Using nested if-else statements to check for each possible range. * `switch`: Using a switch statement with cases for each range. * `amatch-filter`: Using the `filter()` method to find the record that includes the value of `item` in its range. * `amatch-find`: Using the `find()` method to find the first record that matches the condition. **Options Compared** The tests compare two main options: 1. **Filtering vs. Finding**: The `amatch-filter` and `amatch-find` tests use different methods to achieve the same goal: * `filter()`: Returns an array of records that include the value of `item` in their range. * `find()`: Returns the first record that matches the condition (i.e., the entire record if it includes the value of `item`). 2. **Nested Logic**: The `if-else` and `switch` tests use nested logical statements to check for each possible range. **Pros and Cons** Here are some pros and cons of each approach: * `if-else`: + Pros: Easy to understand, straightforward logic. + Cons: Can be slow due to nested checks. * `switch`: + Pros: Concise, fast execution. + Cons: Requires careful handling of cases and edge cases. * `amatch-filter`: + Pros: Fast, efficient using the optimized `filter()` method. + Cons: May not match the expected behavior if the array is large or complex. * `amatch-find`: + Pros: Efficient using the optimized `find()` method. + Cons: May be slower for large arrays due to the search operation. **Other Considerations** When designing performance-critical code, consider factors like: * Cache efficiency * Branch prediction * Loop unrolling * Data locality In this benchmark, the performance difference between the four approaches is likely due to the optimized nature of the `filter()` and `find()` methods in JavaScript. **Conclusion** The benchmark provides a clear comparison of different approaches for finding an item within a range in an array. The results indicate that the optimized `amatch-filter` and `amatch-find` tests outperform the more traditional `if-else` and `switch` approaches, likely due to the efficiency of the optimized methods.
Related benchmarks:
Array slice vs array filter
Array slice vs array filter 1
filter vs for..of
slice vs filter m
slice vs filter for index filtering
Comments
Confirm delete:
Do you really want to delete benchmark?