Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
chain of or equals vs includes
(version: 0)
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:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var stringToMatch = 'hello';
Tests:
Array.includes
['banana', 'map', 'cloth', 'hello', 'person'].includes(stringToMatch)
Or chain
stringToMatch === 'banana' || stringToMatch === 'map' || stringToMatch === 'cloth' || stringToMatch === 'hello' || 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:
7 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.includes
93358184.0 Ops/sec
Or chain
161847120.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of what is tested in the provided JSON benchmark. **Benchmark Overview** The benchmark compares the performance of using `Array.includes` versus manually writing a chain of logical ORs to check if a string matches a specific value within an array. The test aims to measure the overhead of using `Array.includes` compared to traditional OR chaining. **Options Compared** There are two options being tested: 1. **Array.includes**: This is a built-in JavaScript method that checks if a specified element is present in an array. It returns true if the element is found, and false otherwise. 2. **Or chain (manual approach)**: This involves manually writing a chain of logical ORs to check if a string matches a specific value within an array. **Pros and Cons** **Array.includes:** Pros: * Concise and expressive code * Easy to read and maintain * Optimized for performance by the JavaScript engine Cons: * May have overhead due to the additional function call and possible overhead of traversing the array internally * Might not be as efficient as manual OR chaining in some cases, depending on the specific implementation **Or chain (manual approach):** Pros: * Can be more efficient than using `Array.includes` because it avoids the overhead of an additional function call and potentially traversing an array * Gives developers more control over the performance-critical part of the code Cons: * More verbose and harder to read than using `Array.includes` * Requires explicit loop or recursive logic, which can be error-prone **Library Used** In this benchmark, the `stringToMatch` variable is initialized with a string value `'hello'`. No specific library is used in this test case. **Special JS Feature or Syntax** There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives** If you were to implement your own OR chain manually, you could use loops, recursive functions, or even bitwise operations (though that would be a stretch!). Some alternatives to using `Array.includes` include: * Using the `indexOf()` method, which returns the index of the first occurrence of the specified element, and then checking if the index is -1 * Using the `forEach()` method to iterate over the array and check each element manually * Using bitwise operations to compare individual bytes or characters of the string Keep in mind that these alternatives would likely be less efficient than using `Array.includes`, which is optimized for performance by the JavaScript engine. In conclusion, this benchmark provides a simple yet informative way to measure the overhead of using `Array.includes` versus manual OR chaining. By understanding the pros and cons of each approach, developers can make informed decisions about how to optimize their code for performance.
Related benchmarks:
equality vs includes
chain of or equals vs includes but smaller
chain of or equals vs includes 4
equals vs includes
Comments
Confirm delete:
Do you really want to delete benchmark?