<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
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
}
});
_.filter(groupList, { 'group_id': 4 });
_.filter(indexedGroupList, { 'group_id': 4 });
groupList.filter((item) => item?.group_id == 4);
indexedGroupList.filter((item) => item?.group_id == 4);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Filter without index | |
Filter with index | |
Native filter without index | |
Native filter with index |
Test name | Executions per second |
---|---|
Filter without index | 2305.6 Ops/sec |
Filter with index | 2601.7 Ops/sec |
Native filter without index | 2621.4 Ops/sec |
Native filter with index | 2919.3 Ops/sec |
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:
_.filter(indexedGroupList, { 'group_id': 4 });
: Using Lodash's filter
method with an index object.indexedGroupList.filter((item) => item?.group_id == 4);
: Native filter without using a library like Lodash._.filter(groupList, { 'group_id': 4 });
: Using Lodash's filter
method on a separate array of objects.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:
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.Array.prototype.filter
method to filter the array, but does not provide an explicit index object for filtering.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
).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:
filter
method with an index object:filter
method on a separate array of objects:Array.prototype.filter
method on the separate array.Other Considerations
When designing benchmarks like this, consider the following:
I hope this explanation helps you understand the benchmark being tested!