Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
chain of two or equals vs includes
(version: 1)
how much of a performance deficit you can expect from using Array.includes instead of manually writing a chain of logical ORs
Comparing performance of:
Array.includes vs Or chain
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var stringToMatch = 'person';
Tests:
Array.includes
['banana', 'person'].includes(stringToMatch)
Or chain
stringToMatch === 'banana' || stringToMatch === 'person'
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.includes
Or chain
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/136.0.0.0 Safari/537.36 Edg/136.0.0.0
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.includes
96480624.0 Ops/sec
Or chain
138311888.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark described in the provided JSON represents a performance comparison between two different approaches for checking if a specific string is included in a predefined array of strings. The two tested methods are: 1. **Array.includes**: Utilizes the built-in JavaScript Array method `includes()`. This method checks if an array contains a specified element, returning `true` or `false`. 2. **Logical OR Chain**: This approach manually checks if the string matches either of the two individual values by chaining logical OR (`||`) comparisons. In this case, it evaluates if `stringToMatch` is equal to `'banana'` or `'person'`. ### Pros and Cons of Each Approach: #### Array.includes **Pros:** - **Readability**: The intent of checking for inclusion is clear and succinct, which can improve maintainability. - **Semantic Clarity**: It directly conveys that you're looking for an item within an array, resonating well with the intent of the operation. - **Scalability**: For long arrays, using `includes()` may lead to more manageable code compared to long chains of OR comparisons. **Cons:** - **Performance**: In the benchmark, this approach demonstrated a lower execution speed compared to the logical OR chain, potentially making it less suitable for performance-sensitive applications in scenarios with a large number of checks. #### Logical OR Chain **Pros:** - **Performance**: As evidenced by the benchmark results, the logical OR chain resulted in faster execution (138,311,888 executions per second) compared to `Array.includes` (96,480,624 executions per second). This may make it preferable in performance-critical sections of code. - **Control**: Developers have more granularity in defining the comparison logic and can easily modify or extend it beyond mere array inclusions. **Cons:** - **Readability**: As the number of elements to be checked increases, the syntax can become cumbersome and harder to read, diminishing maintainability. - **Scalability**: For large arrays, this approach may not be practical as each additional item requires new logical comparisons, making the code verbose. ### Other Considerations - **Array.includes** is generally a great choice for its clarity and direct purpose when dealing with array membership checks in most typical scenarios, especially when performance is not the primary concern. - The logical OR chain can be particularly useful in small-scale comparisons where performance is critical, and where the array size is known and fixed. ### Alternatives Other alternatives to these two methods could include: - **Set Usage**: If checks for membership are frequent and you're dealing with larger datasets, you can utilize a JavaScript `Set`. It provides an average time complexity of O(1) for lookups, which is efficient: ```javascript const mySet = new Set(['banana', 'person']); mySet.has(stringToMatch); ``` - **Regular Expressions**: For more complex inclusion logic where patterns need to be matched, regular expressions can be employed: ```javascript /banana|person/.test(stringToMatch); ``` Utilizing the correct approach should depend on specific use case scenarios, data structures involved, and performance requirements. For most tasks, `Array.includes` would suffice, but for situations that require utmost efficiency or involve larger sets of data, it would be worth exploring the other alternatives mentioned.
Related benchmarks:
chain of or equals vs includes
chain of or equals vs includes vs equal to many things
equality vs includes
chain of or equals vs includes but smaller
chain of or equals vs includes 4
=== vs includes
String equals vs String.includes
equals vs includes
equals vs includes (one value)
Comments
Confirm delete:
Do you really want to delete benchmark?