Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
if vs includes
(version: 1)
Comparing performance of:
if vs includes
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
/*your preparation JavaScript code goes here To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/ async function globalMeasureThatScriptPrepareFunction() { // This function is optional, feel free to remove it. // await someThing(); }
Tests:
if
const status = 204 if (status === 101 || status === 103 || status === 204 || status === 205 || status === 304) { return true } return false
includes
const status = 204 return [101, 103, 204, 205, 304].includes(status)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
if
includes
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/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
if
165580976.0 Ops/sec
includes
122146208.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark provided in the JSON compares two different approaches to evaluate whether a certain HTTP status code (204 in this case) belongs to a set of specific status codes. The two tested methods are: 1. **Using an `if` statement** 2. **Using the `includes` method on an array** ### Test Cases Explained **1. Using `if` Statement:** ```javascript const status = 204; if (status === 101 || status === 103 || status === 204 || status === 205 || status === 304) { return true; } return false; ``` - **Test Name:** `if` - **Description:** This approach uses a straightforward conditional check with an `if` statement. It explicitly checks if the `status` variable matches any of the predefined values by evaluating multiple equality conditions. **Pros:** - Fast execution due to direct comparison of values. - Clear and easy to understand, especially for developers who might be unfamiliar with array methods. **Cons:** - As the number of conditions increases, the code can become less readable and more cumbersome to manage. - The logic is not easily scalable to larger sets of conditions, requiring maintenance for every change. --- **2. Using `includes` Method:** ```javascript const status = 204; return [101, 103, 204, 205, 304].includes(status); ``` - **Test Name:** `includes` - **Description:** This approach creates an array containing the relevant status codes and checks if the `status` variable is one of the elements in that array using the `Array.prototype.includes` method. **Pros:** - More concise and cleaner syntax when dealing with multiple conditions. - Easier to manage and expand. Adding or removing status codes just requires changing the array. - It abstracts the comparison logic, making it less error-prone when scaling. **Cons:** - Slightly slower than direct `if` checks, especially with larger arrays, due to the overhead of array creation and method invocation. However, performance differences may not be significant with small arrays. ### Benchmark Results From the benchmark results provided, we see the following execution speeds: - **`if` Statement:** 165,580,976 executions per second - **`includes` Method:** 122,146,208 executions per second The `if` method performed better in this specific benchmarking scenario, yielding a higher number of executions per second compared to the `includes` method. ### Considerations and Alternatives While this benchmark offers insights into the performance of these two approaches, it is essential to consider real-world adaptability. A method may be faster in a controlled environment but worse in terms of maintainability or readability in larger codebases. **Other Alternatives:** - **Using a Set**: For very large datasets or frequent membership checks, one could use a JavaScript `Set`, which allows for O(1) average time complexity for checks, unlike arrays which are O(n). The syntax would look like `const statusSet = new Set([101, 103, 204, 205, 304]); return statusSet.has(status);`. - **Using a Switch Statement**: For very specific use cases where you have a limited number of predefined conditions, a `switch` statement could also be considered as an alternative. Overall, each method has its use case based on factors such as performance requirements, code readability, maintainability, and developer familiarity with the syntax features.
Related benchmarks:
Test if if and if else
true vs false vs false vs true
if condition speed test 3
Test if else 2
Test if else 2bis
in function call: if true vs if not return
boolean literal comparison vs typeof js
comparing array creation
Object vs Map MYTEST2137
Comments
Confirm delete:
Do you really want to delete benchmark?