Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash "unionWith" "unionBy" corrected
(version: 0)
Comparing performance of:
unionWith vs unionBy
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:
unionWith
const unionUsers1 = _.unionWith(newUsers, users, (newUser, user) => newUser.name === user.name);
unionBy
const unionUsers2 = _.unionBy(newUsers, users, 'name');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
unionWith
unionBy
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/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
unionWith
91.6 Ops/sec
unionBy
4604.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmarking test cases on MeasureThat.net. **Benchmark Overview** The test measures performance differences between two methods of merging arrays of objects using Lodash, a popular JavaScript utility library. The two methods are: 1. `unionWith` 2. `unionBy` **What is tested?** * **`unionWith`**: This method merges two arrays of objects into a new array by comparing and matching the values in each object based on their keys (i.e., properties). It uses a custom comparison function to determine which key(s) to match. * **`unionBy`**: This method also merges two arrays of objects into a new array, but it uses an existing property as a key for comparison. In this case, the `name` property is used. **Options compared** The benchmark compares the performance of these two methods: Pros and Cons: * **`unionWith`**: + Pros: Provides more control over which keys to match, can be useful when dealing with complex objects. + Cons: Requires a custom comparison function, can be slower due to additional overhead. * **`unionBy`**: + Pros: Easier to use, requires less code and configuration. + Cons: Only compares by one property (`name`), may not work well for more complex data. **Library and purpose** Lodash is a JavaScript utility library that provides various functions for common tasks, including array manipulation (e.g., `unionWith`, `unionBy`). The specific versions used in the benchmark are: * Lodash 4.17.10 The `lodash.min.js` file is included from a CDN to make it easily reproducible. **Special JS features or syntax** None mentioned. **Other alternatives** While not explicitly listed, alternative approaches to merging arrays of objects could include: 1. Using `concat()` and filtering ( manual implementation) 2. Using `Array.prototype.reduce()` with an accumulator 3. Using a custom iterative approach However, these alternatives might be slower and less concise than using Lodash's built-in methods (`unionWith`, `unionBy`). **Benchmark preparation code** The provided script preparation code generates two arrays of objects (users) with random properties and values. It then creates a new array of objects to merge into, which is populated on the fly using an iterative approach. Keep in mind that this code generation might not accurately represent real-world usage scenarios, as it's intended for benchmarking performance. **Latest benchmark result** The latest results show: * `unionBy` outperforms `unionWith` by a significant margin (≈ 50 times faster). These differences are likely due to the custom comparison function and potential overhead in `unionWith`. However, the actual performance impact may vary depending on the specific use case.
Related benchmarks:
Lodash "unionWith" "unionBy" 4
Lodash "unionWith" "unionBy" and isEqual
Lodash "unionWith" "unionBy" "uniqBy" 5
Lodash "uniqWith" "uniqBy" "unionWith" "unionBy"
Comments
Confirm delete:
Do you really want to delete benchmark?