Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash "uniqWith" "unionBy" "uniqBy" 6 Custom
(version: 0)
Comparing performance of:
uniqWith vs unionBy vs uniqBy
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');
uniqBy
const unionUsers1 = _.uniqBy(newUsers.concat(users), p=> p.name + p.age);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
uniqWith
unionBy
uniqBy
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
gemma2:9b
, generated one year ago):
This benchmark tests the performance of three different Lodash functions: `uniqWith`, `unionBy`, and `uniqBy`, each used to deduplicate an array of user objects based on their 'name' property. Here's a breakdown: **Options Compared:** * **`uniqWith`:** This function takes two arguments: the array to deduplicate and a custom comparison function (`_.isEqual` in this case). It uses the provided function to determine if elements are equal. * **`unionBy`:** This function merges two arrays and returns a new array with unique elements based on the specified property ('name' here). It effectively combines the arrays without duplicates, considering the 'name' property as the identifier for uniqueness. * **`uniqBy`:** This function takes an array and a custom comparison function (in this case, a lambda expression `p=> p.name + p.age`) to determine uniqueness. **Pros/Cons:** * **`uniqWith`:** Offers flexibility by allowing custom comparison logic. Can be more performant than other methods if the comparison function is optimized. * **`unionBy`:** Specifically designed for merging arrays while preserving order and considering a specific property for uniqueness. * **`uniqBy`:** Simple and efficient for finding unique elements based on a single property or a combination of properties in a lambda expression. **Library: Lodash** Lodash is a popular JavaScript library that provides utility functions for working with arrays, objects, strings, and more. It simplifies common tasks and offers optimized implementations compared to writing raw JavaScript code. **Other Alternatives:** * **Native JavaScript methods:** You could use `Set` objects or the `filter()` method with custom comparison logic to achieve similar functionality. * **Other libraries:** Libraries like Ramda also provide functions for deduplication and array manipulation. **Choosing the Right Method:** The best choice depends on your specific needs: * If you need fine-grained control over uniqueness determination, use `uniqWith`. * If you're merging arrays and want to maintain order based on a specific property, choose `unionBy`. * For simple deduplication based on one or more properties, `uniqBy` is often the most efficient.
Related benchmarks:
Lodash "unionWith" "unionBy" "uniqWith" 5
Lodash "unionWith" "unionBy" "uniqBy" 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?