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
llama3.1:latest
, generated one year ago):
Let's break down the benchmark test case. **What is being tested:** The benchmark tests two different approaches to filter an array of objects based on whether their IDs match those in another array. **Approach 1: Using `some()` method** In the first approach, a callback function uses the `some()` method to check if any item in the second array (`listOfObjects_2`) has an ID that matches the current item's ID in the first array (`listOfObjects_1`). If such a match is found, the current item is included in the filtered result. **Approach 2: Using `includes()` method** In the second approach, two arrays are created: one containing the IDs of items in `listOfObjects_1` and another containing the IDs to be matched. Then, a callback function uses the `filter()` method to create an array of items from `listOfObjects_2` that have IDs matching those in the first array. **Library used:** None. The test case only utilizes built-in JavaScript methods and functions (e.g., `some()`, `includes()`, `filter()`, `map()`). **JavaScript feature or syntax used:** * Arrow function expressions (`=>`) to define concise callback functions. * Destructuring assignment (`var ids_1 = listOfObjects_1.map(o => o.id)`) to extract IDs from an array of objects. **Pros and cons of the approaches:** Approach 1 (using `some()`): * Pros: + More concise code + Directly uses the `some()` method, which is optimized for performance * Cons: + May have slower performance when dealing with large datasets due to the recursive nature of `some()` Approach 2 (using `includes()`): * Pros: + May be faster when working with large datasets, as it avoids recursion and uses a more efficient lookup mechanism + Easier to understand for developers familiar with array indexing and filtering * Cons: + More verbose code compared to Approach 1 + Requires extra memory to store the ID arrays **Other alternatives:** While not explicitly tested here, other approaches could be: * Using `indexOf()` method ( deprecated in modern browsers) or `findIndex()` method to find an ID match. * Implementing a custom loop-based solution using `for` loops and array iteration. Keep in mind that the actual performance benefits may vary depending on specific use cases, browser versions, and hardware configurations.
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?