Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash reduce to unique items vs Native reduce vs Lodash group and flatten
(version: 0)
compare filter methods on arrays that both modify objects and remove items from the array.
Comparing performance of:
Reduce filter vs Lodash reduce filter vs Lodash group and flatmap
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var testCopy = null; var responsibleRoleId = '5f5d7d26-8551-439f-9c90-3f8e4c52ce8b'; var testArray = [{ id: 'ItemWith Owner 1', name: 'Item With Owner 1', membershipRoleId: '5f5d7d26-8551-439f-9c90-3f8e4c52ce8b' }, { id: 'ItemWith Owner 2', name: 'Item With Owner 2', membershipRoleId: null }, { id: 'ItemWith Owner 2', name: 'Item With Owner 2', membershipRoleId: '5f5d7d26-8551-439f-9c90-3f8e4c52ce8b' }, { id: 'ItemWith Owner 3', name: 'Item With Owner 3', membershipRoleId: '5f5d7d26-8551-439f-9c90-3f8e4c52ce8b' }, { id: 'ItemWith Owner 3', name: 'Item With Owner 3', membershipRoleId: 'b2af40ef-8c6c-44ce-b733-170489104eaa' }, { id: 'ItemWith Owner 4', name: 'Item With Owner 4', membershipRoleId: null }, { id: 'ItemWith Owner 4', name: 'Item With Owner 4', membershipRoleId: '5f5d7d26-8551-439f-9c90-3f8e4c52ce8b' }, { id: 'ItemWith Owner 4', name: 'Item With Owner 4', membershipRoleId: '7af6b8b7-b669-4f6a-b593-3ea7e1c6054d' }, { id: 'ItemWith Owner 5', name: 'Item With Owner 5', membershipRoleId: '5f5d7d26-8551-439f-9c90-3f8e4c52ce8b' }, { id: 'ItemWith Owner 6', name: 'Item With Owner 6', membershipRoleId: null }, { id: 'ItemWith Owner 6', name: 'Item With Owner 6', membershipRoleId: '5f5d7d26-8551-439f-9c90-3f8e4c52ce8b' }, { id: 'ItemWith Owner 6', name: 'Item With Owner 6', membershipRoleId: '3b9c7a19-f047-4cb6-ba12-7b6d0bc0a686' }, { id: 'ItemWithout Owner 1', name: 'Item Without Owner 1', membershipRoleId: null }, { id: 'ItemWithout Owner 2', name: 'Item Without Owner 2', membershipRoleId: '6fee2c1e-9892-452d-98eb-47f516a022bf' }, { id: 'ItemWithout Owner 3', name: 'Item Without Owner 3', membershipRoleId: null }, { id: 'ItemWithout Owner 3', name: 'Item Without Owner 3', membershipRoleId: 'bf5f9c62-c412-4dcc-a1fb-afc66999387d' }, { id: 'ItemWithout Owner 4', name: 'Item Without Owner 4', membershipRoleId: '8a13a6c3-f17c-413c-8c3d-a52264e389fd' }, { id: 'ItemWithout Owner 4', name: 'Item Without Owner 4', membershipRoleId: '51440a3d-52a7-4f02-b583-9bc97009d1db' }, { id: 'ItemWithout Owner 5', name: 'Item Without Owner 5', membershipRoleId: 'cc3db764-bc81-412e-a736-022f36d821a4' }, { id: 'ItemWithout Owner 5', name: 'Item Without Owner 5', membershipRoleId: '74e9b6f9-bdfa-4432-bb5c-690a5506ac74' }];
Tests:
Reduce filter
testCopy = testArray.reduce((acc, v) => { const existingDataIdx = acc.findIndex(i => i.id === v.id); if (existingDataIdx === -1) { return [...acc, v]; } if (acc[existingDataIdx].membershipRoleId !== responsibleRoleId) { acc.splice(existingDataIdx, 1, v); } return acc; }, []);
Lodash reduce filter
testCopy = _.reduce(testArray,(acc, v) => { const existingDataIdx = acc.findIndex(i => i.id === v.id); if (existingDataIdx === -1) { return [...acc, v]; } if (acc[existingDataIdx].membershipRoleId !== responsibleRoleId) { acc.splice(existingDataIdx, 1, v); } return acc; }, []);
Lodash group and flatmap
const uniqueData = []; const groups = _.groupBy(testArray, 'id'); _.flatMap(groups, dataset => { if (dataset.length > 1) { const dataWithResponsible = dataset.find(d => d.membershipRoleId === responsibleRoleId); if (dataWithResponsible) { uniqueData.push(dataWithResponsible); } else { uniqueData.push(dataset[0]); // the responsible values are already reset to null } } else { uniqueData.push(dataset[0]); // the responsible values are already reset to null } }); testCopy = uniqueData;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Reduce filter
Lodash reduce filter
Lodash group and flatmap
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):
The code snippets appear to be JavaScript benchmarking scripts for comparing the performance of two approaches: 1. Using the `reduce()` method in native JavaScript. 2. Using the `_` object from Lodash, specifically the `_reduce()` and `_groupBy()` methods. Here's a brief summary of each code snippet: **Native JavaScript (Reduce Filter)** ```javascript testCopy = testArray.reduce((acc, v) => { const existingDataIdx = acc.findIndex(i => i.id === v.id); if (existingDataIdx === -1) { return [...acc, v]; } if (acc[existingDataIdx].membershipRoleId !== responsibleRoleId) { acc.splice(existingDataIdx, 1, v); } return acc; }, []); ``` This code uses the `reduce()` method to iterate through the `testArray` and filter out items that don't match a specific condition. The `findIndex()` method is used to find the index of an existing item with the same `id`, and then the corresponding item is removed from the accumulator array if its `membershipRoleId` doesn't match the expected value. **Lodash (Reduce Filter)** ```javascript testCopy = _.reduce(testArray,(acc, v) => { const existingDataIdx = acc.findIndex(i => i.id === v.id); if (existingDataIdx === -1) { return [...acc, v]; } if (acc[existingDataIdx].membershipRoleId !== responsibleRoleId) { acc.splice(existingDataIdx, 1, v); } return acc; }, []); ``` This code uses the `_reduce()` method from Lodash to achieve the same result as the native JavaScript implementation. The main difference is that Lodash provides a more concise and expressive way of writing this type of logic. **Lodash (Group and Flatten)** ```javascript const uniqueData = []; const groups = _.groupBy(testArray, 'id'); _.flatMap(groups, dataset => { if (dataset.length > 1) { const dataWithResponsible = dataset.find(d => d.membershipRoleId === responsibleRoleId); if (dataWithResponsible) { uniqueData.push(dataWithResponsible); } else { uniqueData.push(dataset[0]); } } else { uniqueData.push(dataset[0]); } }); testCopy = uniqueData; ``` This code uses the `_groupBy()` method to group the items in `testArray` by their `id`, and then applies the `_flatMap()` method to flatten the resulting groups into a single array. The main difference is that this approach requires more intermediate variables, making it slightly less efficient than the native JavaScript implementation. The latest benchmark results show that: * Native JavaScript (Reduce Filter) executed approximately 675,638 times per second. * Lodash (Reduce Filter) executed approximately 614,411 times per second, which is about 9% slower than the native JavaScript implementation. * Lodash (Group and Flatten) executed approximately 387,670 times per second, which is about 42% slower than the native JavaScript implementation. Please note that these results are likely influenced by various factors, such as the specific hardware and software environment in which they were run.
Related benchmarks:
Lodash reduce filter vs Native reduce filter vs Lodash filter vs Native filter
lodash groupBy vs Array.reduce 100k corrected
lodash groupBy vs Array.reduce vs Array.group 100k
Lodash difference vs filter & some
Comments
Confirm delete:
Do you really want to delete benchmark?