Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash "unionWith" "unionBy" "uniqBy" 5
(version: 0)
Comparing performance of:
uniqWith vs unionBy vs unionWith2
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Script Preparation code:
var users = [ { 'name': 'barney', 'age': 36 }, { 'name': 'pebbles', 'age': 5 } ]; var newUsers = [ { 'name': 'fred', 'age': 40 }, { 'name': 'barney', 'age': 36 }, { 'name': 'pebbles', 'age': 2 } ]; for(let i = 0; i < 2000; i++) { var newUser = { 'name': 'user_' + i, 'age': Math.floor(Math.random() * i) }; newUsers.push(newUser); }
Tests:
uniqWith
const unionUsers1 = _.uniqWith(newUsers.concat(users), _.isEqual);
unionBy
const unionUsers2 = _.unionBy(newUsers, users, 'name');
unionWith2
const unionUsers1 = _.uniqBy(newUsers.concat(users), 'name');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
uniqWith
unionBy
unionWith2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
uniqWith
2.0 Ops/sec
unionBy
9017.0 Ops/sec
unionWith2
9565.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of JavaScript microbenchmarks can be an intriguing topic. **Overview** The provided benchmark measures the execution time of three different methods for merging and processing arrays of objects using the Lodash library: 1. `uniqBy` (Union By): Merges two arrays and returns a new array with unique elements based on the specified property. 2. `unionWith` (Union With): Merges two arrays and returns a new array with all elements, using a custom function to determine uniqueness. 3. `uniqBy` alternative: Similar to `uniqBy`, but uses an alternative implementation. **Description of each approach** ### 1. `uniqBy` (Union By) * **Purpose:** Merges two arrays and returns a new array with unique elements based on the specified property (`'name'` in this case). * **Functionality:** `_unionBy` iterates through both arrays, checks for uniqueness using `_.isEqual`, and adds unique elements to the result. * **Pros:** + Efficient implementation of union-by-based merging. + Simplifies code by using a single function for all operations. * **Cons:** + May not perform well with large datasets due to the need to check for equality between elements. + Uses `_.isEqual` which can be slow if used on non-primitive values. ### 2. `unionWith` (Union With) * **Purpose:** Merges two arrays and returns a new array with all elements, using a custom function (`_.isEqual`) to determine uniqueness. * **Functionality:** `_unionWith` iterates through both arrays, checks for uniqueness using the custom function, and adds unique elements to the result. * **Pros:** + Flexibility allows users to customize the uniqueness check using their own function. + Can handle large datasets efficiently if the custom function is optimized. * **Cons:** + Requires a custom implementation of the uniqueness check, which can be error-prone. + May not be as efficient as `uniqBy` due to the need for an additional function. ### 3. Alternative `uniqBy` * **Purpose:** Similar to `uniqBy`, but uses an alternative implementation (no specific details provided). * **Pros:** + Possibly optimized for performance compared to the original `uniqBy`. + May have other advantages over the standard implementation. * **Cons:** + No clear information on how it differs from the standard `uniqBy`. **Library usage** The benchmark uses the Lodash library, which provides various utility functions, including: * `_unionBy`: Merges two arrays and returns a new array with unique elements based on the specified property. * `_unionWith`: Merges two arrays and returns a new array with all elements, using a custom function to determine uniqueness. **Special JavaScript features** The benchmark does not specifically mention any special JavaScript features or syntax. However, it assumes that the user has access to modern JavaScript environments and Lodash library versions. **Other alternatives** If you need to merge arrays in a different way or require more flexibility in your uniqueness check, you can consider using other libraries like `lodash.merge` or implementing custom array merging functions.
Related benchmarks:
Lodash "unionWith" "unionBy" "uniqWith" 5
Lodash "uniqWith" "unionBy" "uniqBy" 6
Lodash "uniqWith" "unionBy" "uniqBy" 27062023
Lodash "uniqWith" "unionBy" "uniqBy" 8
Comments
Confirm delete:
Do you really want to delete benchmark?