Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash "uniqWith" "unionBy" "uniqBy" 8
(version: 0)
Comparing performance of:
uniqWith vs uniqBy
Created:
2 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);
uniqBy
const unionUsers1 = _.uniqBy(newUsers.concat(users), (val) => val.toString());
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:
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):
**Overview of the Benchmark** The provided benchmark is designed to test the performance of two functions in Lodash: `uniqWith` and `uniqBy`. The test aims to determine which function is more efficient for unionizing arrays with varying levels of complexity. **What is being tested?** In this benchmark, the following functions are being compared: 1. `_.uniqWith(users.concat(newUsers), _.isEqual)`: This function uses `_.isEqual` as a comparison function to check for equality between objects in the input arrays. 2. `_.uniqBy(newUsers.concat(users), (val) => val.toString())`: This function uses a custom comparison function that converts each object value to a string using `toString()`. **Pros and Cons of each approach** 1. **`.uniqWith(_.isEqual)`** * Pros: + Simple and straightforward implementation + Fast performance since it only checks for reference equality between objects * Cons: + May not work well with non-object values or complex data structures + May be slower than `.uniqBy` due to the overhead of creating a comparison function 2. **`.uniqBy((val) => val.toString())`** * Pros: + Can handle any type of value, including non-objects and complex data structures + Often faster performance since it avoids the need for expensive equality checks * Cons: + Requires converting each object value to a string, which can lead to slower performance if not done efficiently + May not work well with large datasets or high-performance requirements **Lodash libraries and functions** In this benchmark, Lodash is used as the external library. The `_.uniqWith` and `_.uniqBy` functions are part of the Lodash utility library. **Other considerations** * The test uses a synthetic dataset generated by JavaScript code to create two large arrays of user objects. * The `concat()` method is used to combine the input arrays, which can lead to slower performance compared to using `push()` or other methods that avoid creating intermediate arrays. * The benchmark is run on a Desktop platform with Chrome 116 browser, which may not be representative of all possible environments. **Alternatives** Some alternative approaches for unionizing arrays include: 1. Using the `Set` data structure: Create a Set from each input array and then merge them using `Set.union()`. 2. Using `Array.prototype.reduce()` or `Array.prototype.forEach()`: Iterate over one of the input arrays and add elements to the other array. 3. Using a custom implementation with loops: Write a loop-based implementation that iterates over both input arrays and adds unique elements to a result array. These alternatives may offer different trade-offs in terms of performance, readability, and maintainability compared to using `_.uniqWith` or `_.uniqBy`.
Related benchmarks:
Lodash "unionWith" "unionBy" "uniqWith" 5
Lodash "unionWith" "unionBy" "uniqBy" 5
Lodash "uniqWith" "unionBy" "uniqBy" 6
Lodash "uniqWith" "unionBy" "uniqBy" 27062023
Comments
Confirm delete:
Do you really want to delete benchmark?