Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
searching_new22
(version: 0)
comparing if-else vs. switch vs. filter/includes vs. find/includes
Comparing performance of:
if-else vs object vs more obj
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var item = 999; var moreData = { 10: "less than 20", 11: "less than 20", 12: "less than 20", 13: "less than 20", 14: "less than 20", 15: "less than 20", 16: "less than 20", 17: "less than 20", 18: "less than 20", 19: "less than 20", 20: "less than 30", 21: "less than 30", 22: "less than 30", 23: "less than 30", 24: "less than 30", 25: "less than 30", 26: "less than 30", 27: "less than 30", 28: "less than 30", 29: "less than 30", 30: "less than 40", 31: "less than 40", 32: "less than 40", 33: "less than 40", 34: "less than 40", 35: "less than 40", 36: "less than 40", 37: "less than 40", 38: "less than 40", 39: "less than 40", 40: "less than 50", 41: "less than 50", 42: "less than 50", 43: "less than 50", 44: "less than 50", 45: "less than 50", 46: "less than 50", 47: "less than 50", 48: "less than 50", 49: "less than 50", }; var setData = []; for (let i=10; i<20; i++) { setData[i] = "less than 20"; } for (let i=20; i<30; i++) { setData[i] = "less than 30"; } for (let i=30; i<40; i++) { setData[i] = "less than 40"; } for (let i=40; i<50; i++) { setData[i] = "less than 50"; }
Tests:
if-else
if (item < 10) { console.log("less than 10"); return; } else if (item < 20) { console.log("less than 20"); return; } else if (item < 30) { console.log("less than 30"); return; } else if (item < 40) { console.log("less than 40"); return; } else if (item < 50) { console.log("less than 50"); return; } else { console.log("50 or more!"); return; }
object
console.log(moreData[item] || ((item < 10) ? 'less than 10' : '50 or more!'));
more obj
console.log(setData[item] || ((item < 10) ? 'less than 10' : '50 or more!'));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
if-else
object
more obj
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 provide an in-depth explanation of the provided benchmark. **Overview** The benchmark compares four approaches to make decisions based on conditions: 1. **If-Else Statements** 2. **Switch Statement** 3. **Array Filter/Includes Method** 4. **Array Find/Includes Method** Each approach is tested with three test cases: "object", "more obj", and "if-else". **Approach 1: If-Else Statements** If-else statements are a fundamental control structure in JavaScript that allow the program to execute different blocks of code based on conditions. Pros: * Easy to understand and implement * Well-supported by most browsers Cons: * Can be cumbersome for complex decisions or large datasets * May lead to slower performance due to repeated checks **Approach 2: Switch Statement** Switch statements are another way to make decisions in JavaScript. They allow the program to execute different blocks of code based on a specific value. Pros: * More concise than if-else statements for simple decisions * Can be faster than if-else statements for repeated checks Cons: * May not work well with complex decisions or large datasets * Requires the value to be explicitly defined in the switch statement **Approach 3: Array Filter/Includes Method** Array methods can be used to filter and check elements in an array. Pros: * Can be concise for simple filtering operations * Fast and efficient, especially with modern browsers' optimized array methods Cons: * May require additional setup and understanding of the array's context * Not designed for complex decisions or large datasets **Approach 4: Array Find/Includes Method** Array methods can also be used to find specific elements in an array. Pros: * Fast and efficient, especially with modern browsers' optimized array methods * Can be concise for simple search operations Cons: * May require additional setup and understanding of the array's context * Not designed for filtering or complex decisions **Test Cases** The benchmark tests each approach with three test cases: * "object": Tests a direct lookup in an object. * "more obj": Tests a lookup in an object that has more data, similar to the `setData` array defined in the benchmark definition. * "if-else": Tests a traditional if-else statement. **Library and Special Features** None of the test cases use any external libraries. However, they do utilize modern JavaScript features such as arrow functions, template literals, and optimized array methods (like `includes()`). **Benchmark Results** The benchmark results show the executions per second for each approach on a specific browser and device platform: * "more obj": 20977.240234375 executions/second * "object": 20925.91796875 executions/second * "if-else": 18925.208984375 executions/second These results suggest that the Array Filter/Includes Method (or more specifically, the `includes()` method) is the fastest approach for these test cases, followed closely by direct object lookup (`object` test case). The if-else statement is slower due to repeated checks. However, it's essential to note that these results may vary depending on the specific browser, device platform, and other factors.
Related benchmarks:
searching_new
searching_new2
Lodash difference vs filter and includes on large arrays
indexOf vs. Includes vs. Find vs. Some vs. Filter vs. loop vs. $.inArray()
Comments
Confirm delete:
Do you really want to delete benchmark?