Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
chain of or equals vs includes vs equal to many things
(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 vs Or chain of equality
Created:
3 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'
Or chain of equality
stringToMatch === ('banana' || 'map' || 'cloth' || 'hello'|| 'person')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.includes
Or chain
Or chain of equality
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.includes
7106845.5 Ops/sec
Or chain
2503411.5 Ops/sec
Or chain of equality
9296598.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases to understand what is being tested. **What is being tested?** The benchmark is designed to measure the performance difference between three approaches: 1. `Array.includes` (the original approach) 2. A chain of logical ORs (`stringToMatch === 'banana' || stringToMatch === 'map' || ...`) 3. A single expression using the OR operator with equality checks (`stringToMatch === ('banana' || 'map' || ...')`) **Options being compared:** * `Array.includes`: This is a built-in method in JavaScript that checks if a specific value exists in an array. * Chain of logical ORs: This approach uses multiple OR operators to check if the `stringToMatch` variable matches any of the values in the array. * Single expression using OR operator with equality checks: This approach uses a single expression to achieve the same result as the chain of logical ORs. **Pros and Cons of each approach:** 1. `Array.includes`: * Pros: Efficient, simple, and widely supported. * Cons: May not be optimized for performance in all browsers or situations. 2. Chain of logical ORs: * Pros: Can be useful when the array contains a small number of values. * Cons: Can lead to slower performance due to the need for multiple checks. 3. Single expression using OR operator with equality checks: * Pros: Combines the benefits of both approaches, potentially offering better performance and readability. * Cons: May not be as widely supported or optimized as `Array.includes`. **Library usage:** None explicitly mentioned in this benchmark. **Special JS feature/syntax:** The use of template literals (`\r\n\t`) is a recent syntax feature introduced in ECMAScript 2015. It allows embedding expressions inside string literals, making it easier to write readable code. **Benchmark preparation code and test cases:** The script preparation code defines a single variable `stringToMatch` with the value `'hello'`. Each test case defines a different benchmark: 1. The first test case uses `Array.includes` to check if the `stringToMatch` value exists in an array containing several values. 2. The second test case uses a chain of logical ORs to achieve the same result as the previous test case. 3. The third test case uses a single expression using the OR operator with equality checks to achieve the same result. **Other alternatives:** If you wanted to explore alternative approaches, you could consider: * Using `some()` method instead of `Array.includes`, which can be more efficient in certain cases. * Utilizing specialized libraries or frameworks for benchmarking JavaScript performance. * Implementing a custom solution using bitwise operations or other optimization techniques.
Related benchmarks:
chain of or equals vs includes
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?