Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Some vs Find for non existence check
(version: 0)
Comparing performance of:
Find vs Some
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = [] for (let i = 0; i < 5000; ++i) data.push({ username: 'toto' }) data.push({ username: 'titi' }) for (let i = 0; i < 2500; ++i) data.push({ username: 'toto' })
Tests:
Find
data.find(e => e.username === 'titi') !== undefined
Some
!data.some(e => e.username === 'titi')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Find
Some
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark on MeasureThat.net compares two ways of checking if an element exists in an array: using `find()` and using `some()`. **`find()` Approach:** - **Description:** Searches the array for an element that satisfies the provided condition (in this case, `e => e.username === 'titi'`). It returns the *first* matching element or `undefined` if no match is found. - **Pros:** Returns the specific element if found, potentially useful if you need to work with the matched item further. - **Cons:** Iterates through the entire array even after finding a match, potentially less efficient if you only care about existence. **`some()` Approach:** - **Description:** Checks if *at least one* element in the array satisfies the provided condition (`e => e.username === 'titi'`). It returns `true` if a match is found and `false` otherwise. - **Pros:** Stops iterating as soon as a match is found, making it more efficient when you only need to know if an element exists. - **Cons:** Doesn't return the matched element itself; you only get a boolean result. **Other Considerations:** * **Data Structure:** The benchmark uses an array of objects. `find()` and `some()` are generally good choices for arrays, but consider other data structures like maps if key lookup is faster. * **Array Size:** For small arrays, the performance difference between these methods might be negligible. For larger arrays, `some()`'s early exit can lead to significant speed gains. **Alternatives:** - **`includes()`:** A more direct alternative for checking existence by value in an array (but doesn't work with objects). Let me know if you have any more questions!
Related benchmarks:
some vs find low number of data
Some vs Find fedfd
Some vs Find bool
Some vs Find early find
Comments
Confirm delete:
Do you really want to delete benchmark?