Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash "uniqWith" "unionBy" "uniqBy" 6
(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), 'name');
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:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
uniqWith
2.2 Ops/sec
unionBy
9816.4 Ops/sec
uniqBy
10419.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark. **Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark test case, which compares different approaches for merging arrays using the Lodash library. The benchmark consists of three test cases: 1. `uniqWith` 2. `unionBy` 3. `uniqBy` Each test case uses the Lodash library to merge two arrays: `users` and `newUsers`. The purpose of this exercise is to determine which approach performs better in terms of execution speed. **Lodash Library** The Lodash library is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, object creation, and more. In this benchmark, we're using the `uniqWith`, `unionBy`, and `uniqBy` functions from the Lodash library. * `uniqWith`: This function removes duplicate elements in an array based on a provided comparison function. * `unionBy`: This function merges two arrays into one, removing duplicates based on a common property (in this case, 'name'). * `uniqBy`: This function also removes duplicate elements in an array, but it uses the 'name' property as the key for comparison. **Options Compared** The three test cases compare the following approaches: 1. `uniqWith`: Uses the `_.isEqual` function to compare elements, which checks for strict equality. 2. `unionBy`: Merges two arrays into one, removing duplicates based on the 'name' property of each object in the array. 3. `uniqBy`: Removes duplicate elements from an array, using the 'name' property as the key for comparison. **Pros and Cons of Each Approach** Here's a brief summary of the pros and cons of each approach: 1. `uniqWith`: * Pros: Can handle more complex comparisons (not limited to strict equality). * Cons: May be slower due to the additional overhead of comparing elements. 2. `unionBy`: * Pros: Efficiently merges two arrays, removing duplicates based on a common property. * Cons: Limited to merging arrays based on a single property ('name' in this case). 3. `uniqBy`: * Pros: Removes duplicate elements from an array efficiently, using the 'name' property as the key for comparison. * Cons: Limited to removing duplicates from an array; not suitable for merging two arrays. **Other Considerations** When choosing between these approaches, consider the specific requirements of your use case. For example: * If you need to perform complex comparisons or handle irregular data structures, `uniqWith` might be a better choice. * If you only need to merge two arrays based on a common property, `unionBy` is likely sufficient. * If you want to remove duplicates from an array efficiently, using the 'name' property as the key for comparison, `uniqBy` is probably the best option. **Alternatives** If you're looking for alternative approaches or libraries for performing similar operations, consider the following: * For merging arrays based on a common property: You can use the `Array.prototype.reduce()` method or a custom implementation using `forEach()` and `find()`. * For removing duplicates from an array: You can use the `Set` object or implement a simple algorithm like `indexOf()` and `push()` to remove duplicates. * For complex comparisons or data manipulation: Consider using other JavaScript libraries like Ramda, Underscore.js, or even vanilla JavaScript functions like `Array.prototype.filter()`, `Array.prototype.map()`, and `Array.prototype.sort()`.
Related benchmarks:
Lodash "unionWith" "unionBy" "uniqWith" 5
Lodash "unionWith" "unionBy" "uniqBy" 5
Lodash "uniqWith" "unionBy" "uniqBy" 27062023
Lodash "uniqWith" "unionBy" "uniqBy" 8
Comments
Confirm delete:
Do you really want to delete benchmark?