Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash "uniqWith" "uniqBy" 7
(version: 0)
Comparing performance of:
uniqBy vs uniqWith
Created:
one year 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:
uniqBy
const unionUsers1 = _.uniqBy(newUsers, (a, b) => a.name === b.name && a.age === b.age);
uniqWith
const unionUsers1 = _.uniqWith(newUsers, v => [v.name, v.age].join());
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
uniqBy
uniqWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
uniqBy
0.0 Ops/sec
uniqWith
13775.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain the provided benchmark. **What is being tested?** The provided JSON represents two JavaScript microbenchmarks that test the `uniqWith` and `uniqBy` functions from the Lodash library. These functions are used to remove duplicate elements from an array based on a custom comparison function. **Options compared:** The two options being compared are: 1. **Lodash `uniqWith`**: This function removes duplicates from an array by providing a custom comparison function that combines multiple fields of each object into a single value. 2. **Lodash `uniqBy`**: This function is similar to `uniqWith`, but it only considers the first field (i.e., the first property) of each object for comparison. **Pros and Cons:** * Lodash `uniqWith`: + Pros: - Allows for more flexibility in defining the comparison logic. - Can be useful when multiple fields need to be considered for uniqueness. + Cons: - May be slower due to the complexity of the comparison function. - Requires a custom comparison function, which can add extra overhead. * Lodash `uniqBy`: + Pros: - Faster execution time since only one field needs to be compared. - Simplifies the code by not requiring a custom comparison function. + Cons: - Less flexible than `uniqWith`, as it only considers the first field. **Library and its purpose:** The Lodash library is a popular utility library that provides a wide range of functional programming helpers, including the `uniqWith` and `uniqBy` functions. These functions are designed to make it easier to work with arrays and objects in JavaScript, often by providing concise and expressive solutions to common problems. **Special JS feature or syntax:** There doesn't seem to be any special JavaScript features or syntax being used in this benchmark that would require specific knowledge of those features. **Benchmark preparation code explanation:** The provided Script Preparation Code generates two arrays of user objects with random properties. One array (`newUsers`) is used as input for both `uniqWith` and `uniqBy`, while the other array (`users`) is not used in this benchmark. **Other alternatives:** If you wanted to implement these functions without using Lodash, you could use JavaScript's built-in `Set` data structure or a simple iterative approach with an object to keep track of unique elements. However, this would likely be less efficient and more verbose than using Lodash's optimized implementations. Keep in mind that the choice between `uniqWith` and `uniqBy` ultimately depends on your specific use case and performance requirements. If you need more flexibility in defining the comparison logic, `uniqWith` might be a better choice. Otherwise, `uniqBy` is likely to provide faster execution times due to its simplified implementation.
Related benchmarks:
Lodash "uniqWith" "unionBy" "uniqBy" 6
Lodash "uniqWith" "unionBy" "uniqBy" 27062023
Lodash "uniqWith" "unionBy" "uniqBy" 8
Lodash "uniqWith" "uniqBy" 8
Comments
Confirm delete:
Do you really want to delete benchmark?