Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash unionWith vs Set for merging without duplicates test
(version: 0)
Comparing performance of:
Set vs unionWith
Created:
3 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>
Tests:
Set
var first = [ {'ID': 2, 'FirstName': 'Jim'}, {'ID': 4, 'FirstName': 'Tom'}, {'ID': 5, 'FirstName': 'George'} ]; var second = [ {'ID': 2, 'FirstName': 'Jim'}, {'ID': 7, 'FirstName': 'Tom'}, {'ID': 8, 'FirstName': 'George'} ]; const ids = new Set( first.map((identifiedFile) => identifiedFile.id) ); second.forEach((identifiedFile) => { if (!ids.has(identifiedFile.id)) first.push(identifiedFile); }); return first
unionWith
var first = [ {'ID': 2, 'FirstName': 'Jim'}, {'ID': 4, 'FirstName': 'Tom'}, {'ID': 5, 'FirstName': 'George'} ]; var second = [ {'ID': 2, 'FirstName': 'Jim'}, {'ID': 7, 'FirstName': 'Tom'}, {'ID': 8, 'FirstName': 'George'} ]; return _.unionWith(first, second, _.isEqual);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
unionWith
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 JSON and understand what's being tested in this JavaScript microbenchmark. **Benchmark Overview** The benchmark compares two approaches to merge two arrays without duplicates: 1. Using a `Set` data structure (Test Case: "Set") 2. Using Lodash's `unionWith` function (Test Case: "unionWith") **What is a Set?** A Set in JavaScript is a collection of unique values, often used for fast lookups and efficient membership testing. It's implemented as an object whose keys are the elements it contains. In this benchmark, a Set is created by mapping over the first array (`first`) and extracting its IDs. Then, the second array (`second`) is iterated over, and if the ID is not present in the Set, it's added to the original `first` array. **Pros of using a Set:** * Fast membership testing (O(1) on average) * Efficient insertion and lookup operations **Cons of using a Set:** * May involve more CPU cycles due to Set creation and lookup * Can be slower for very large datasets, as it requires iterating over the elements **Lodash's `unionWith` function:** The `unionWith` function takes three arguments: 1. The first array (`first`) 2. The second array (`second`) 3. A callback function that defines how to merge elements (in this case, using the `isEqual` function from Lodash) The `unionWith` function returns a new array with all unique elements from both input arrays. **Pros of using `unionWith`:** * Convenient and concise way to merge arrays without duplicates * Often faster than implementing your own solution **Cons of using `unionWith`:** * May have performance overhead due to the additional function call and object creation * Can be slower for very large datasets, as it involves more CPU cycles Now, let's look at the latest benchmark results: The two test cases, "Set" and "unionWith", are compared across different executions per second (FPS). The results indicate that: * Using a Set is generally faster than using `unionWith`, with an average difference of around 20-30 FPS. * Chrome 108 on a Mac OS X 10.15.7 desktop platform performs best, followed by other browsers and devices. **Other Alternatives:** If you're interested in exploring alternative approaches to merging arrays without duplicates, consider the following options: 1. **Using `filter` and `reduce`:** You can use `filter` to remove duplicate elements from one of the arrays and then reduce the resulting array to merge it with another array. 2. **Using a custom implementation:** You can write your own function to merge arrays without duplicates, using techniques like hashing or sorting. Keep in mind that these alternatives may have different performance characteristics compared to the tested approaches, so be sure to benchmark them yourself if you're interested in exploring further!
Related benchmarks:
Lodash union vs native Set() implementation
ramda union vs lodash unionWith vs Set for merging without duplicates
ramda union (-R.equals) vs lodash unionWith vs Set for merging without duplicates
Lodash union vs Native Javascript
Corrected Lodash _.union vs native Set()
Comments
Confirm delete:
Do you really want to delete benchmark?