Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
includes vs if else
(version: 0)
Comparing performance of:
includes vs if else
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
let tempArr = [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], [ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ], [ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 ], [ 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 ], [ 41, 42, 43, 44, 45, 46 ] ]
Tests:
includes
let tempArr = [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], [ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ], [ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 ], [ 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 ], [ 41, 42, 43, 44, 45, 46 ] ] let tc1 = tempArr.filter((arr) => arr.includes(28))[0]
if else
let tempArr = [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], [ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ], [ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 ], [ 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 ], [ 41, 42, 43, 44, 45, 46 ] ] let tc2 = tempArr.filter((arr) => (28 > arr[0] && 28 < arr[9]))[0]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
if else
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
23644936.0 Ops/sec
if else
53936324.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the benchmark. **Benchmark Overview** The benchmark compares two approaches for filtering an array of arrays: using `Array.prototype.includes()` and using an if-else statement. **Includes vs If Else Approach** In the "includes" approach, a single method call is made on each sub-array to check if it contains the value 28. This approach is concise and efficient. In the "if else" approach, two separate condition checks are performed: one for checking if the first element of the sub-array is greater than 28 and another for checking if the last element of the sub-array is less than 28. This approach is more verbose but can be useful in certain situations where a single check is not sufficient. **Pros and Cons** * **Includes Approach:** + Pros: - Concise and efficient. - Reduces branching and can lead to better performance due to cache locality. + Cons: - May not work correctly if the value being searched for is not present in the array (returns false instead of undefined). * **If Else Approach:** + Pros: - Can be more readable and maintainable, especially when dealing with complex conditions. - Works correctly even if the value being searched for is not present in the array (returns false instead of undefined). + Cons: - More verbose and less efficient due to branching. **Other Considerations** * The benchmark uses a simple test case with an array of arrays containing consecutive integers. In a real-world scenario, the input data might be more complex, affecting performance and readability. * The if else approach requires additional checks for edge cases, such as when the value being searched for is not present in the array or when the sub-arrays have varying lengths. **Library Usage** There are no external libraries used in this benchmark. **Special JS Feature/Syntax** The benchmark uses a few special JavaScript features: * Template literals (`\r\n` and `\n`) to create multiline strings. * Array destructuring (`arr[0]`, `arr[9]`) to access array elements. Overall, the benchmark provides a simple yet informative comparison of two approaches for filtering arrays. It highlights the trade-offs between conciseness and performance, as well as readability and maintainability.
Related benchmarks:
includes vs ifelse
spread vs concat vs unshift vs flat v2
filtering with Array.prototype.flatMap vs for of loop vs Array.prototype.map + Array.prototype.filter
Indexof vs Includes in an array
Comments
Confirm delete:
Do you really want to delete benchmark?