Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
searching_new
(version: 0)
compating if-else vs. switch vs. find/includes
Comparing performance of:
if-else vs switch vs amatch
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var item = 45; var data = [ // { range: [1,2,3,4,5,6,7,8,9], value: "less than 10" }, { 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" }, ]; 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 // Below is BETTER that above as it stops on first finding!!!! return (data.find(record => record.range.includes(item))).value };
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
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 // Below is BETTER that above as it stops on first finding!!!! return (data.find(record => record.range.includes(item))).value }; amatch(item);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
if-else
switch
amatch
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):
I'll dive right into explaining the benchmark and its various aspects. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark that compares three different approaches for searching within an array of objects: `if-else`, `switch`, and a custom function called `amatch`. The benchmark aims to determine which approach is more efficient in terms of execution speed. **Comparison of Approaches** 1. **If-Else**: This approach uses a chain of nested if-statements to check the value of the `item` variable against different ranges in the `data` array. The first matching condition returns the corresponding value. * Pros: Simple, easy to understand, and works well for small arrays or specific use cases. * Cons: Can be slow for large arrays or when searching for a specific value that doesn't fit into any range. 2. **Switch**: This approach uses a switch statement with multiple cases to check the value of the `item` variable against different ranges in the `data` array. The first matching case returns the corresponding value. * Pros: Similar to if-else, but can be slightly faster due to the more efficient lookup mechanism. * Cons: Can be less readable for complex cases or when dealing with multiple values. 3. **Amatch**: This is a custom function that uses `find` and `includes` methods to search for the value within the array. It stops searching as soon as it finds a match, which can improve performance. * Pros: More efficient than if-else and switch due to its optimized algorithm and use of built-in methods. * Cons: May have higher memory usage due to the creation of an iterator. **Library Usage** In this benchmark, no libraries are explicitly mentioned. However, it's worth noting that `includes` is a part of the JavaScript standard library since ES6. **Special JS Features/Syntax** There are no special JS features or syntax used in this benchmark, apart from the use of arrow functions (`=>`) which is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). **Other Alternatives** If you were to rewrite this benchmark using other approaches, here are some alternatives: * **For-Loops**: Instead of if-else or switch, you could use for-loops to iterate over the array and find the matching value. * **Regular Expressions**: You could use regular expressions to search for patterns in the array values. This approach would be more suitable for searching strings rather than numbers. * **Binary Search**: If the array is sorted, you could use binary search algorithms to find the matching value more efficiently. Keep in mind that each alternative will have its own trade-offs in terms of performance, readability, and maintainability.
Related benchmarks:
searching_new2
searching_new22
find faster - for vs find vs map vs foreach II
Slice vs Splice vs Shift (100)
Comments
Confirm delete:
Do you really want to delete benchmark?