Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.filter with large arrays of objects
(version: 2)
Testing whether filter an array of large objects is faster is we "index" the objects vs just having an array of large objects.
Comparing performance of:
Filter without index vs Filter with index vs Native filter without index vs Native filter with index
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
Script Preparation code:
var indexedGroupList = Array.from({length: 50000}, () => { var temp_id = Math.floor(Math.random() * 99999999); var temp_group_id = Math.floor(Math.random() * 10); return { "id": temp_id, "group_id": temp_group_id, "post": { "id": temp_id, "group_id": temp_group_id, "name": "Test Name Here", "created_at": 1234871928373, "updated_at": 8471829378473, "is_test": true, "is_valid": false, "description": "this is a long form description text here.", "description2": "this is a long form description text here.this is a long form description text here.this is a long form description text here.this is a long form description text here.", "description3": "this is a long form description text here.this is a long form description text here.this is a long form description text here.this is a long form description text here.this is a long form description text here.this is a long form description text here.this is a long form description text here.", "description4": "this is a long form description text here.", "description5": "this is a long form description text here.", "description6": "this is a long form description text here.", "is_test2": true, "is_valid2": false, "is_test3": true, "is_valid3": false, "is_test4": true, "is_valid4": false } } }); var groupList = Array.from({length: 50000}, () => { return { "id": Math.floor(Math.random() * 99999999), "group_id": Math.floor(Math.random() * 10), "name": "Test Name Here", "created_at": 1234871928373, "updated_at": 8471829378473, "is_test": true, "is_valid": false, "description": "this is a long form description text here.", "description2": "this is a long form description text here.this is a long form description text here.this is a long form description text here.this is a long form description text here.", "description3": "this is a long form description text here.this is a long form description text here.this is a long form description text here.this is a long form description text here.this is a long form description text here.this is a long form description text here.this is a long form description text here.", "description4": "this is a long form description text here.", "description5": "this is a long form description text here.", "description6": "this is a long form description text here.", "is_test2": true, "is_valid2": false, "is_test3": true, "is_valid3": false, "is_test4": true, "is_valid4": false } });
Tests:
Filter without index
_.filter(groupList, { 'group_id': 4 });
Filter with index
_.filter(indexedGroupList, { 'group_id': 4 });
Native filter without index
groupList.filter((item) => item?.group_id == 4);
Native filter with index
indexedGroupList.filter((item) => item?.group_id == 4);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Filter without index
Filter with index
Native filter without index
Native filter with index
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.2:3b
, generated one year ago):
I'll provide an in-depth explanation of the benchmark being tested, the options compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark measures the performance of filtering large arrays of objects using different approaches: 1. `_.filter(indexedGroupList, { 'group_id': 4 });`: Using Lodash's `filter` method with an index object. 2. `indexedGroupList.filter((item) => item?.group_id == 4);`: Native filter without using a library like Lodash. 3. `_.filter(groupList, { 'group_id': 4 });`: Using Lodash's `filter` method on a separate array of objects. 4. `groupList.filter((item) => item?.group_id == 4);`: Native filter without using a library like Lodash. **Options Compared** The benchmark compares the performance of four different approaches: 1. **Using Lodash's `filter` method with an index object**: This approach uses the `_.filter` method to filter the array, passing an index object (`{ 'group_id': 4 }`) as a parameter. 2. **Native filter without using a library like Lodash**: This approach uses the built-in `Array.prototype.filter` method to filter the array, but does not provide an explicit index object for filtering. 3. **Using Lodash's `filter` method on a separate array of objects**: This approach uses the `_.filter` method on a separate array of objects (`groupList`) instead of the indexed array (`indexedGroupList`). 4. **Native filter without using a library like Lodash (separate array)**: Similar to the previous approach, but uses the built-in `Array.prototype.filter` method on the separate array of objects. **Pros and Cons** Here's a brief summary of the pros and cons for each approach: 1. **Using Lodash's `filter` method with an index object**: * Pros: Easy to implement, efficient use of Lodash's filtering capabilities. * Cons: Requires importing Lodash library, may not be optimized for performance in all cases. 2. **Native filter without using a library like Lodash**: * Pros: No additional library dependencies, can be optimized for specific use cases. * Cons: Requires manual implementation of filtering logic, may be less efficient than using a library like Lodash. 3. **Using Lodash's `filter` method on a separate array of objects**: * Pros: Simplifies filtering logic by using a separate array, can reduce overhead. * Cons: May not take advantage of optimizations available when filtering the original array directly. 4. **Native filter without using a library like Lodash (separate array)**: * Pros: Similar to the previous approach, but uses the built-in `Array.prototype.filter` method on the separate array. * Cons: Requires manual implementation of filtering logic, may be less efficient than using a library like Lodash. **Other Considerations** When designing benchmarks like this, consider the following: 1. **Test data size**: The benchmark tests large arrays of objects to simulate real-world scenarios. Adjusting the test data size can help you understand how performance changes with varying input sizes. 2. **Platform and browser support**: The benchmark runs on a specific platform (Mac OS X 10.15) and browser (Firefox 95). Testing on other platforms and browsers can provide a more comprehensive understanding of the results. 3. **Optimizations and caching**: Depending on the implementation, certain optimizations or caching mechanisms may be applied to improve performance. Make sure to test these scenarios if they're relevant to your use case. I hope this explanation helps you understand the benchmark being tested!
Related benchmarks:
filter vs foreach vs for
_.filter with large objects 2
Some vs. Filter vs. findIndex vs find
Reduce with spread VS for...of with push
Comments
Confirm delete:
Do you really want to delete benchmark?