Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash "unionWith" "unionBy" "uniqWith" 5
(version: 0)
Comparing performance of:
uniqWith vs unionBy vs unionWith2
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, (user) => user.name);
unionWith2
const unionUsers1 = _.unionWith(newUsers, users, (newUser, user) => newUser.name === user.name);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
uniqWith
unionBy
unionWith2
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 benchmark and explain what's being tested. **Benchmark Definition:** The benchmark consists of two main functions from the Lodash library: 1. `unionBy`: This function returns a new array with elements from both arrays, filtering out duplicates based on a custom comparison function (in this case, the `name` property of each object). 2. `unionWith`, `uniqWith`, and `unionWith2` are variations of this function. The benchmark creates two arrays: `users` and `newUsers`. It then uses these arrays to create new functions that perform union operations with a custom comparison function. **Options Compared:** Three options are being compared: 1. `unionBy`: This function filters out duplicates based on the `name` property of each object. 2. `unionWith2`: This function also filters out duplicates, but uses a different comparison function (`newUser.name === user.name`) to compare objects. 3. `uniqWith`: This function removes duplicate elements from an array without filtering on any specific property (i.e., it's not comparable). **Pros and Cons:** Here are some pros and cons of each approach: 1. **unionBy**: Pros: * Easy to implement * Fast performance Cons: * Requires a custom comparison function, which can be brittle if not implemented correctly. 2. **unionWith2**: Pros: * More flexible than `unionBy` since it allows for more complex comparisons using the provided callback function. Cons: * Slightly slower performance compared to `unionBy`. 3. **uniqWith**: Pros: * Simple and easy to implement Cons: * Not suitable for filtering based on specific properties (e.g., `name`, `age`). * May be slower than `unionBy` or `unionWith2`. **Library and Its Purpose:** The Lodash library is a popular utility library that provides functional programming helpers, including the `_.uniqWith` function. The purpose of `uniqWith` is to remove duplicate elements from an array while preserving the original order. **Special JS Feature/Syntax:** There are no specific JavaScript features or syntax used in this benchmark that require special attention. However, it's worth noting that the use of Lodash library and its functions is a common pattern in JavaScript development. **Other Alternatives:** If you don't have access to the Lodash library or want to implement these functions yourself, here are some alternative approaches: 1. `unionBy`: * Use a simple array filter with a custom comparison function. 2. `unionWith2`: * Implement your own comparison function using JavaScript's built-in functions (e.g., `===`, `!==`, `includes()`, etc.). 3. `uniqWith`: * Use a simple array filter without any specific properties (i.e., just remove duplicates). Here are some example implementations: ```javascript // unionBy function unionBy(arr1, arr2) { return arr1.filter((item1) => !arr2.includes(item1)); } // unionWith2 function unionWith2(arr1, arr2) { return arr1.reduce((acc, item1) => { const match = arr2.find((item2) => item2.name === item1.name); if (match) acc.push(match); else acc.push(item1); return acc; }, []); } // uniqWith function uniqWith(arr) { return arr.filter((item, index, self) => { const first = self.indexOf(item); return first === index; }); } ``` Keep in mind that these implementations may have different performance characteristics compared to using the Lodash library functions.
Related benchmarks:
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?