Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
has vs find
(version: 0)
Comparing performance of:
find vs has
Created:
4 years ago
by:
Registered User
Jump to the latest result
Tests:
find
const a = [1,2,3,4,5] a.find(x => x === 5);
has
const a = new Set([1,2,3,4,5]) a.has(5);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
find
has
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/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
find
155486304.0 Ops/sec
has
21862550.0 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark tests the performance of two ways to check if an element exists within a collection: `find` and `has`. **Here's a breakdown:** * **Test Case 1: `find`**: The code snippet searches for the value `5` within an array `[1,2,3,4,5]`. The `find` method iterates through the array until it finds the matching element and returns that element. It stops iterating once a match is found. * **Test Case 2: `has`**: This case utilizes a `Set`, which is a collection designed for efficient membership checking. The code checks if the value `5` exists within the set `[1,2,3,4,5]`. Sets have constant-time lookups (`O(1)`), making them very fast for this kind of operation. **Pros and Cons:** * **find**: * **Pro**: Flexible; can process elements and potentially modify them during the search before returning a match. * **Con**: Can be slower than `has` because it iterates through the entire collection if no match is found immediately. * **has**: * **Pro**: Extremely fast due to constant-time lookups in Sets. * **Con**: Less flexible; primarily designed for checking membership and doesn't allow processing elements during the search. **Library**: None used in these test cases. They utilize built-in JavaScript array (`find`) and Set methods. **Alternatives:** * **Arrays `includes` method**: Similar to `find` but returns a boolean indicating if the element is present, rather than the element itself. * **Iterating manually**: You could loop through the collection yourself using a `for` loop or a `forEach` function. However, this is generally less efficient and readable compared to built-in methods. Let me know if you have any other questions.
Related benchmarks:
Array.prototype.find vs Lodash find 2
set.has vs. array.find
set vs array find if exists v2
#2 Array Includes vs. Find
Comments
Confirm delete:
Do you really want to delete benchmark?