Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash "unionWith"
(version: 0)
Comparing performance of:
unionWith vs sortedUniqBy and sortBy
Created:
6 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:
unionWith
const unionUsers1 = _.unionWith(newUsers, users, (newUser, user) => newUser.name === user.name);
sortedUniqBy and sortBy
const unionUsers2 = _.sortedUniqBy(_.sortBy([...newUsers, ...users], u => u.name), u => u.name);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
unionWith
sortedUniqBy and sortBy
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.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark, called "Lodash `unionWith`", tests the performance of two methods from the Lodash library: `unionWith` and `sortedUniqBy`/`sortBy`. These methods are used to merge arrays or perform set operations. **Test Cases** There are two test cases: 1. **`unionWith`**: This method takes three arguments: * `users`: The original array. * `newUsers`: The new array to be merged with `users`. * A callback function that checks if the name of a new user matches an existing user in `users`. The test case creates two large arrays, `users` and `newUsers`, and then runs the `unionWith` method on them 2000 times. The benchmark measures how many executions per second this code can perform. 2. **`sortedUniqBy`/`sortBy`**: This is a combination of two methods: `sortedUniqBy` and `sortBy`. * `sortedUniqBy`: Removes duplicates from an array while maintaining order, based on the provided callback function. * `sortBy`: Sorts an array based on the provided callback function. The test case first sorts both arrays (`users` and `newUsers`) by name using `sortBy`, then removes duplicates from the combined array using `sortedUniqBy`. The benchmark measures how many executions per second this code can perform. **Options Compared** In the `unionWith` method, two options are compared: * Using a callback function to compare elements. * Not using a callback function (i.e., simply checking for equality of names). The choice of option affects performance: + Using a callback function provides more control and can be faster in some cases, but it also adds overhead due to the additional function call. + Not using a callback function is simpler and might be faster, but it may not provide the same level of control. In the `sortedUniqBy`/`sortBy` combination, two options are compared: * Using `sortedUniqBy` followed by `uniq` (not shown in this benchmark). * Using `sortedUniqBy`/`sortBy` combined into a single method call (`_.sortedUniqBy(_.sortBy([...newUsers, ...users], u => u.name), u => u.name)`). The choice of option affects performance: + Using `sortedUniqBy` followed by `uniq` might be faster since it avoids the overhead of sorting and then removing duplicates. + The combined method call is simpler but may have slightly higher overhead due to the additional function call. **Pros and Cons** Here are some pros and cons for each approach: * **`unionWith` with callback function**: Pros - more control, might be faster. Cons - adds overhead due to callback function call. * **`unionWith` without callback function**: Pros - simpler, potentially faster. Cons - less control over comparison logic. * **`sortedUniqBy`/`sortBy` combination**: Pros - simplified and potentially faster than using `sortedUniqBy` followed by `uniq`. Cons - might have slightly higher overhead due to additional function call. **Lodash Libraries** In this benchmark, the following Lodash libraries are used: * `lodash.min.js`: The main Lodash library. * `_.unionWith`: A method that merges two arrays while applying a callback function for duplicate checking. * `_.sortedUniqBy`: A method that removes duplicates from an array while maintaining order and based on a provided callback function. **Special JavaScript Features or Syntax** This benchmark does not use any special JavaScript features or syntax. It focuses solely on the performance of Lodash methods. I hope this explanation helps!
Related benchmarks:
Lodash "unionWith" "unionBy" 4
Lodash "unionWith" "unionBy" "uniqBy" 5
Lodash "uniqWith" "unionBy" "uniqBy" 27062023
Lodash "uniqWith" "unionBy" "uniqBy" 8
Lodash "unionWith" "unionBy" corrected
Comments
Confirm delete:
Do you really want to delete benchmark?