Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ramda union (-R.equals) vs lodash unionWith vs Set for merging without duplicates
(version: 0)
Comparing performance of:
Set vs _.unionWith vs R.union
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js'></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js'></script>
Tests:
Set
const first = [ {a: 3, b: 3, c: "hey"}, {a: 3, b: 2, c: "hey"}, {a: 1, b: 2, c: "bye"}, {a: 3, b: 4, c: "hey"}, {a: 2, b: 1, c: "bye"} ]; const second = [ {a: 3, b: 4, c: "hey"}, {a: 2, b: 1, c: "bye"}, {a: 4, b: 4, c: "sup"}, {a: 3, b: 3, c: "hey"} ]; return [...new Set([...first, ...second])]
_.unionWith
const first = [ {a: 3, b: 3, c: "hey"}, {a: 3, b: 2, c: "hey"}, {a: 1, b: 2, c: "bye"}, {a: 3, b: 4, c: "hey"}, {a: 2, b: 1, c: "bye"} ]; const second = [ {a: 3, b: 4, c: "hey"}, {a: 2, b: 1, c: "bye"}, {a: 4, b: 4, c: "sup"}, {a: 3, b: 3, c: "hey"} ]; return _.unionWith(first, second, _.isEqual);
R.union
const first = [ {a: 3, b: 3, c: "hey"}, {a: 3, b: 2, c: "hey"}, {a: 1, b: 2, c: "bye"}, {a: 3, b: 4, c: "hey"}, {a: 2, b: 1, c: "bye"} ]; const second = [ {a: 3, b: 4, c: "hey"}, {a: 2, b: 1, c: "bye"}, {a: 4, b: 4, c: "sup"}, {a: 3, b: 3, c: "hey"} ]; return R.union(first, second);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set
_.unionWith
R.union
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):
I'll dive into explaining the benchmark and its various aspects. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark, where three different approaches are compared: using `Set`, `_unionWith` from Lodash, and `R.union` from Ramda. The goal of this benchmark is to measure which approach performs better in terms of execution speed when merging two arrays without duplicates. **Options Compared** The three options being compared are: 1. **Set**: Using the built-in `Set` data structure in JavaScript to remove duplicates. 2. **_.unionWith` from Lodash**: A utility function from the popular Lodash library that merges two arrays while removing duplicates using a custom callback function. 3. **R.union` from Ramda**: A higher-order function from the Ramda library that applies a unifying function to multiple iterables, effectively merging and removing duplicates. **Pros and Cons of Each Approach** 1. **Set**: * Pros: Built-in, efficient, and widely supported in modern browsers. * Cons: Requires extra memory usage for the `Set` object and may have higher overhead due to object creation. 2. _.unionWith` from Lodash: * Pros: Convenient and readable syntax using a callback function to handle duplicate elements. * Cons: Requires an additional library (Lodash), which may not be included in all projects, and may introduce slight performance overhead. 3. R.union` from Ramda: * Pros: Higher-order function with functional programming benefits, allowing for more expressive and composable code. * Cons: Requires another external library (Ramda) and may have a slightly higher memory footprint. **Other Considerations** When using `Set`, it's essential to be aware of the potential issues: * If the input arrays contain large numbers of unique elements, the `Set` object might consume significant memory. * In some cases, the order of elements in the resulting array might not match the original order due to the way `Set` works. The use of _.unionWith and R.union provides a more functional programming approach, which can be beneficial for code readability and maintainability. However, it may come at the cost of slightly slower performance compared to using built-in data structures like `Set`. **Library Use** In this benchmark, two external libraries are used: * Lodash (`_.unionWith`): A popular utility library providing a wide range of functional programming helpers. * Ramda (`R.union`): A higher-order function library that provides a more expressive and composable way to work with data. These libraries can be useful in various JavaScript projects, but their inclusion may add overhead or dependents in the project's dependency tree. **Special JS Feature/Syntax** None of the benchmark definitions explicitly use special JavaScript features or syntax. The code is straightforward and uses standard JavaScript constructs. **Alternatives** If you're looking for alternative approaches to merging arrays without duplicates, some other options include: * Using a custom function with a simple `for...in` loop and an object to keep track of unique elements. * Utilizing the spread operator (`...`) in combination with the `Set` data structure. * Employing a more advanced data structure like a trie or a suffix tree for efficient duplicate detection. Keep in mind that these alternatives might have different trade-offs regarding performance, memory usage, and code readability.
Related benchmarks:
Lodash union vs native Set() implementation
ramda union vs lodash unionWith vs Set for merging without duplicates
Deep merge: lodash vs ramda vs Object spread
Lodash union vs Native Javascript
Comments
Confirm delete:
Do you really want to delete benchmark?