Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash "uniqWith" "uniqBy"
(version: 1)
Comparing performance of:
uniqWith vs uniqBy
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.40/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);
uniqBy
const unionUsers1 = _.uniqBy(newUsers.concat(users), (user) => user.name + user.age);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
uniqWith
uniqBy
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/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
uniqWith
0.6 Ops/sec
uniqBy
571.2 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON evaluates the performance of two Lodash functions—`uniqWith` and `uniqBy`—specifically in their ability to deduplicate an array of user objects. Here's an overview of what each function does, the comparison being made, the advantages and drawbacks of each approach, and additional considerations. ### Benchmark Overview 1. **Dataset Preparation**: - The benchmark begins by preparing datasets using the `Script Preparation Code`. - `users` and `newUsers` arrays are created, with `newUsers` containing a mix of predefined users and randomly generated entries. 2. **Test Cases**: - Two different methods are tested: - **uniqWith**: ```javascript const unionUsers1 = _.uniqWith(newUsers.concat(users), _.isEqual); ``` This function deduplicates the combined `newUsers` and `users` arrays based on strict equality comparison, determined by `_.isEqual`. - **uniqBy**: ```javascript const unionUsers1 = _.uniqBy(newUsers.concat(users), (user) => user.name + user.age); ``` This function also deduplicates the arrays but does so based on a computed key derived from each object's `name` and `age` properties. ### Performance Comparison From the benchmark results: - **Performance Metrics**: - **uniqBy**: Approximately **571.19 executions per second**. - **uniqWith**: Approximately **0.61 executions per second**. ### Pros and Cons 1. **uniqWith**: - **Pros**: - Flexibility in comparing complex objects. It can effectively handle cases where a straightforward property comparison isn't sufficient. - Makes use of deep equality checks, which may be important for certain applications. - **Cons**: - Significantly slower than `uniqBy` as it compares objects deeply. - More CPU-intensive, affecting performance as the dataset size grows. 2. **uniqBy**: - **Pros**: - Much faster performance, as it generates a string key for each object and compares these keys rather than the objects themselves. - More efficient in cases where you can derive a unique key based on defined properties. - **Cons**: - Requires that the properties used to create the key be sufficient to ensure uniqueness, which may not hold true in all scenarios. ### Other Considerations - **Usage of Lodash**: Lodash is a utility library that provides a set of functions for common programming tasks, such as manipulating arrays, numbers, objects, strings, etc. It aids in writing concise and high-performance JavaScript code. - **Alternatives**: - **Native JavaScript**: For modern applications, features like the spread operator and `Set` can be used to achieve similar deduplication without a library. However, implementations need to ensure that unique checks are done correctly. ```javascript const uniqueUsers = [...new Map(combinedUsers.map(user => [user.name + user.age, user])).values()]; ``` - **Using a Custom Deduplication Function**: Depending on specific requirements, developers may implement custom deduplication logic tailored to their needs, taking performance and object structure into account. ### Summary This benchmark provides valuable insights into the performance differences between two approaches for deduplication in JavaScript. While `uniqWith` offers comprehensive equality checks suitable for complex data structures, `uniqBy` stands out for its superior performance due to key-based deduplication. Developers should consider the context of their application to choose the most suitable method.
Related benchmarks:
Lodash "unionWith"
Lodash "uniqWith" "unionBy" "uniqBy" 6
Lodash "uniqWith" "unionBy" "uniqBy" 6 Custom
Lodash "uniqWith" "unionBy" "uniqBy" (100)
Lodash "unionBy"vs "uniqBy"
Lodash "uniqWith" "unionBy" "uniqBy" 27062023
Lodash "uniqWith" "unionBy" "uniqBy" 8
Lodash "uniqWith" "unionBy" "uniqBy" yummy
Lodash "uniqWith" "uniqBy" 7
Comments
Confirm delete:
Do you really want to delete benchmark?