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
llama3.1:latest
, generated one year ago):
Let's dive into the explanation. **Benchmark Overview** The benchmark is testing the performance of three different methods for removing duplicates from an array of objects, using the Lodash library. The methods being compared are: 1. `uniqWith` (`.uniqWith()` function) 2. `unionBy` (`.unionBy()` function) 3. `uniqBy` (`.uniqBy()` function) **Test Cases** Each test case is a separate benchmark that measures the execution time of one of these methods. ### Test Case 1: `uniqWith` * **Method**: _.uniqWith(newUsers.concat(users), _.isEqual); * **Library**: Lodash's `.uniqWith()` function * **Purpose**: Removes duplicates from an array by using a custom equality test function (`_.isEqual`). * **Options compared**: Not explicitly mentioned, but likely comparing the performance of using `_.uniqWith` with a custom equality test (`.isEqual`) versus other methods. * **Pros/Cons**: Using `.uniqWith` with a custom equality test can be efficient if you have a specific comparison function. However, it may not be as scalable or performant for large datasets compared to other methods. * **Alternative approaches**: + Use `Set` data structure (built-in JavaScript) to remove duplicates without the need for a library like Lodash. ### Test Case 2: `unionBy` * **Method**: _.unionBy(newUsers, users, 'name'); * **Library**: Lodash's `.unionBy()` function * **Purpose**: Returns a new array with only unique elements based on a specified property (`'name'`). * **Options compared**: Not explicitly mentioned, but likely comparing the performance of using `_.unionBy` versus other methods. * **Pros/Cons**: Using `.unionBy` can be efficient when you need to remove duplicates based on a specific key. However, it may not be as flexible or performant for complex comparison functions. * **Alternative approaches**: + Use a combination of filtering and grouping (e.g., `reduce()` function) to achieve similar results without Lodash. ### Test Case 3: `uniqBy` * **Method**: _.uniqBy(newUsers.concat(users), p=> p.name + p.age); * **Library**: Lodash's `.uniqBy()` function * **Purpose**: Returns a new array with only unique elements based on a custom key generator (`p => p.name + p.age`). * **Options compared**: Not explicitly mentioned, but likely comparing the performance of using `_.uniqBy` versus other methods. * **Pros/Cons**: Using `.uniqBy` can be efficient when you need to remove duplicates based on a complex comparison function. However, it may not be as scalable or performant for large datasets compared to other methods. * **Alternative approaches**: + Use a combination of filtering and grouping (e.g., `reduce()` function) to achieve similar results without Lodash. In summary, the benchmark is testing the performance of three different methods from the Lodash library for removing duplicates from an array of objects. The results show that `unionBy` outperforms the other two methods in this specific test case. If you're not using Lodash or don't need its features, alternative approaches like using built-in JavaScript data structures (e.g., Sets) or implementing custom solutions can be more efficient and scalable.
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?