Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
chain of or equals vs Set.has
(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:
Set.has vs Or chain
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var stringToMatch = 'hello';
Tests:
Set.has
new Set(['banana', 'map', 'cloth', 'hello', 'person']).has(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
Set.has
Or chain
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0
Browser/OS:
Firefox 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set.has
2121971.8 Ops/sec
Or chain
870845376.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and its pros and cons. **Benchmark Definition** The test case measures the performance difference between two approaches to check if a string matches certain values: 1. Using `Set.has` method (a built-in JavaScript function) on a Set data structure. 2. Writing a chain of logical ORs (`||`) to achieve the same result. **Approaches compared:** * **Array includes vs. manual or chaining**: This test case is often referred to as "the Or chain vs. Array.includes benchmark" or "String matching with or chain vs. Set.has". **Pros and Cons:** * **Set.has method (recommended)**: + Pros: - Faster execution time. - More concise code. - Less prone to errors (since it's a built-in function). + Cons: - May not be supported by older browsers or versions of JavaScript. * **Manual or chaining (`||`)**: + Pros: - Works in all modern browsers and versions of JavaScript. - Can be useful for understanding how the `Array.includes` method works internally. + Cons: - Less efficient than using `Set.has`. - More prone to errors due to the possibility of incorrect order or omitted values. **Other considerations:** * The use of a Set data structure is likely due to its optimized internal implementation, which can lead to better performance compared to other data structures like arrays. * Using `Set.has` also avoids the overhead of searching for specific values in an array, as it uses a hash-based lookup mechanism under the hood. **Library and special JS features used:** There are no libraries explicitly mentioned. However, note that using `Array.includes` (the approach being compared to Set.has) relies on the internal implementation of arrays in JavaScript, which is an optimized data structure provided by the language itself. **Individual test cases:** Each test case measures the execution time of a specific scenario: 1. **Set.has**: Measures the performance of checking if `stringToMatch` exists within the given Set using the `Set.has` method. 2. **Or chain (`||`)**: Measures the performance of writing a chain of logical ORs to check if `stringToMatch` matches any of the values in the array. **Alternatives:** * Other alternatives for checking string matching could include: + Using other data structures, such as Tries or Trie-Set. + Implementing custom algorithms using bitwise operations or regular expressions. + Utilizing specialized libraries like RegEx (which is not explicitly mentioned but might be considered by some developers). Keep in mind that the most efficient approach will depend on the specific requirements and constraints of your project, such as performance considerations or compatibility with older browsers.
Related benchmarks:
chain of or equals 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?