Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array filtering with some vs array filtering with includes
(version: 0)
Return only objects with same id
Comparing performance of:
array filtering with some vs array filtering with includes
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var listOfObjects_1 = [{id: 1, name: '1'}, {id: 2, name: '2'}, {id: 5, name: '5'}] var listOfObjects_2 = [{id: 1, name: '1'}, {id: 2, name: '2'}, {id: 5, name: '4'}]
Tests:
array filtering with some
listOfObjects_1.filter(selectedItem => listOfObjects_2.some(item => item.id === selectedItem.id) )
array filtering with includes
var ids_1 = listOfObjects_1.map(o => o.id) listOfObjects_2.filter(o => ids_1.includes(o.id))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array filtering with some
array filtering with includes
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 compares two different methods of filtering an array in JavaScript: **Method 1: `filter` with `some`:** * The code uses the `filter()` method on the first array (`listOfObjects_1`). * For each object in `listOfObjects_1`, it checks if any object in the second array (`listOfObjects_2`) has an identical `id`. The `some()` method is used for this check. If a matching `id` is found, the object is included in the filtered result. **Method 2: `filter` with `includes`:** * This method first creates a new array (`ids_1`) containing only the `id` values from `listOfObjects_1`. * Then, it uses the `filter()` method on `listOfObjects_2`, checking if each object's `id` exists in the `ids_1` array using the `includes()` method. **Pros and Cons:** * **Method 1 (`some`)**: * **Pro:** Can be more expressive as it directly checks for a match within another array. * **Con:** Potentially slower because `some()` iterates through the entire second array for each object in the first array. * **Method 2 (`includes`)**: * **Pro:** Potentially faster because `includes()` is generally optimized and only iterates until a match is found or the end of the array. * **Con:** Requires an extra step to create the `ids_1` array, which might add a negligible overhead. **Alternatives:** * **Set**: You could use Sets to efficiently check for existence of IDs: - Create a Set from `listOfObjects_1`'s IDs. Then filter `listOfObjects_2` by checking if each object's ID is present in the set. * **Hash Map (Object):** Use an Object to store IDs and their corresponding values from `listOfObjects_1`. Then, iterate through `listOfObjects_2` and check for the presence of IDs in this hash map. Let me know if you have any other questions!
Related benchmarks:
lodash intersectionBy vs array filtering with includes
IndexOf vs Includes vs find
lodash intersectionWith vs array filtering with includes
equality vs includes
Comments
Confirm delete:
Do you really want to delete benchmark?