Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array filtering with some vs array filtering with includes (1000 elements)
(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:
function createObjectsArray(n) { let arr = []; const salt=(Math.random() + '').slice(2,3); while(n > 0) { arr.push({ id: n+salt, name: 'name' + n + salt }) n--; } return arr; } //var listOfObjects_1 = [{id: 1, name: '1'}, {id: 2, name: '2'}, {id: 5, name: '5'}] var listOfObjects_1 = createObjectsArray(1000) //var listOfObjects_2 = [{id: 1, name: '1'}, {id: 2, name: '2'}, {id: 5, name: '4'}] var listOfObjects_2 = createObjectsArray(1000)
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 tests two different approaches for filtering an array of objects based on matching IDs. **Test Case 1: `array filtering with some`** * **Benchmark Definition:** `listOfObjects_1.filter(selectedItem => listOfObjects_2.some(item => item.id === selectedItem.id))` * **Explanation:** This approach iterates through each object in `listOfObjects_1`. For every object, it checks if an object with the same ID exists in `listOfObjects_2` using the `.some()` method. If a match is found, the current object from `listOfObjects_1` is included in the filtered result. * **Pros:** * Potentially efficient for smaller arrays because `.some()` stops searching once it finds a match. * **Cons:** * Can be less efficient than `.includes()` if many objects in `listOfObjects_1` don't have matching IDs in `listOfObjects_2`. **Test Case 2: `array filtering with includes`** * **Benchmark Definition:** `var ids_1 = listOfObjects_1.map(o => o.id)\r\nlistOfObjects_2.filter(o => ids_1.includes(o.id))` * **Explanation:** This approach first extracts all the IDs from `listOfObjects_1` and stores them in a new array (`ids_1`). Then, it filters `listOfObjects_2`, keeping only objects whose IDs exist in the `ids_1` array using the `.includes()` method. * **Pros:** * Can be more efficient than `.some()` for larger arrays because `includes()` has optimized lookup times for known values within an array. * **Cons:** * Requires an extra step to create the `ids_1` array, potentially adding a slight overhead. **Other Considerations** * **Array Size:** For very large arrays, the efficiency difference between these approaches might become more noticeable. * **Distribution of IDs:** If the IDs in `listOfObjects_1` and `listOfObjects_2` are not evenly distributed, one approach might be more efficient than the other depending on the data patterns. Let me know if you have any further questions!
Related benchmarks:
Set vs Filter vs includes+push for unique
Set vs Filter for unique 40k
Set vs Filter for unique with 10000 items
Set vs Filter for unique for me
Filter vs Set (unique elements)
Comments
Confirm delete:
Do you really want to delete benchmark?