Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
chain of or equals vs includes (2 object items)
(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:
const a = {} const b = {}
Tests:
Array.includes
[b, a].includes(a)
Or chain
b === a || a === a
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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.includes
103802320.0 Ops/sec
Or chain
172866704.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark tests the performance difference between two methods of checking equality among two objects: using `Array.includes` versus a manual chain of logical `OR` statements. ### Benchmark Overview - **Benchmark Name**: Chain of `OR` equals vs `includes` (2 object items) - **Description**: This benchmark evaluates the performance deficit incurred by using `Array.includes` compared to a manually written series of logical `OR` comparisons. ### Options Compared 1. **Array.includes**: - **Code**: `[b, a].includes(a)` - **Description**: This method checks if the array contains the object `a`. `Array.includes` is a built-in JavaScript method that returns `true` or `false` based on the presence of the specified value in the array. 2. **Manual Or Chain**: - **Code**: `b === a || a === a` - **Description**: This approach directly checks if `b` is equal to `a` or if `a` is equal to itself (which will always be true). It uses straightforward logical comparisons without the overhead of an array method. ### Pros and Cons #### Array.includes - **Pros**: - Readability: Clear and expressive, especially for developers unfamiliar with the logic. - Maintainability: Easy to modify if the number of objects or conditions increases. - **Cons**: - Performance: The implementation of `includes` involves iterating over the array until the condition is satisfied, which can result in overhead. - It may not be as efficient for a small, fixed number of comparisons, particularly since this benchmark checks only two objects. #### Manual Or Chain - **Pros**: - Performance: Direct comparison is generally faster as it involves no iteration or additional function calls. - Low overhead: This approach is simple and performs well with a small number of comparisons. - **Cons**: - Readability: As the number of conditions increases, it may lead to less readable code. - Error-prone: Manual chaining may be more susceptible to logical errors if not constructed carefully. ### Additional Considerations In scenarios where the number of checked objects increases, the manual approach can become cumbersome and less maintainable. In such cases, using structured data handling (like arrays) becomes more favorable despite the potential performance trade-off. ### Alternative Methods 1. **Using `Set`**: If unique identities are necessary, leveraging `Set` for lookup can offer a balance between performance and clarity, although it may not apply directly if you need to match two specific objects (since `Set` checks for reference equality). 2. **`Array.indexOf`**: An older method for checking existence in an array, but less preferred than `includes` due to its less clear intent and functionality. 3. **Custom Utility Function**: Building your own utility function to handle multiple comparisons can also be a viable alternative, which can manage both readability and performance based on the specific use case. In conclusion, this benchmark highlights the trade-offs between performance and readability/maintainability when choosing between native array methods and manual comparisons in JavaScript. Each method has its context of suitability, and the choice often depends on the specific needs of the application being developed.
Related benchmarks:
.includes vs .some
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
=== vs includes
IF vs Array.includes()
equals vs includes
equals vs includes (one value)
Comments
Confirm delete:
Do you really want to delete benchmark?