Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash _.union vs native Set() spread
(version: 0)
Comparing performance of:
_.union vs Set() vs Set() convert back to array
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
Script Preparation code:
var a = [1, 2, 3, 4, 5] var b = [3, 4, 5, 6, 7]
Tests:
_.union
var c = _.union(a, b)
Set()
var c = new Set(a, b)
Set() convert back to array
var c = new Set(a, b) var d = [...c]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.union
Set()
Set() convert back to array
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 compares the performance of three approaches for performing set operations in JavaScript: 1. Using Lodash's `_.union` function 2. Creating a new Set object and using its `add()` method to add elements from two arrays 3. Creating a new Set object, adding elements from two arrays, and then converting it back to an array using the spread operator (`[...]`) **Library: Lodash** Lodash is a popular JavaScript utility library that provides a set of reusable functions for common tasks, including array manipulation. In this benchmark, Lodash's `_.union` function is used to compare its performance with native Set operations. The `_.union` function takes two arrays as input and returns a new array containing all unique elements from both arrays. **Native Set Operations** Creating a new Set object allows you to efficiently store and look up elements. The `add()` method is used to add elements to the set, which has an average time complexity of O(1). Converting a Set back to an array using the spread operator (`[...]`) has a time complexity of O(n), where n is the number of elements in the set. **Performance Comparison** The benchmark results show that: * Creating a new Set object and adding elements from two arrays (option 2) is the fastest approach, with an average execution speed of approximately 316,7891.0 executions per second. * Converting a Set back to an array using the spread operator (`[...]`) (option 3) is slower than option 2 but still relatively fast, with an average execution speed of approximately 16,378,910.0 executions per second. * Using Lodash's `_.union` function (option 1) is the slowest approach, with an average execution speed of approximately 3,167,911.0 executions per second. **Pros and Cons** Here are some pros and cons for each approach: * **Option 2: Creating a new Set object** + Pros: - Fastest approach - Most efficient use of memory - Cons: - Requires manual addition of elements using `add()` method * **Option 3: Converting a Set to an array using the spread operator** + Pros: - Easy to implement and understand - No need to manually manage element addition - Cons: - Slower than option 2 - Requires extra memory for creating a new array * **Option 1: Using Lodash's `_.union` function** + Pros: - Easy to implement and understand + Cons: - Slowest approach - Requires an additional library dependency **Other Alternatives** If you're looking for alternative approaches, consider the following: * **Using Array.prototype.reduce()`**: This method allows you to perform set operations using a more functional programming style. However, it may be slower than option 2 or option 3. * **Using Array.prototype.concat()` and `Set`**: This approach combines the benefits of options 2 and 3 by creating a new Set object and then concatenating an array of values using `concat()`. However, it may still be slower than option 2. In conclusion, when performing set operations in JavaScript, consider using native Set objects (option 2) for optimal performance. If you prefer a more functional programming style or need to handle large datasets efficiently, explore alternative approaches like Array.prototype.reduce() or using an array with a Set object.
Related benchmarks:
_.union vs native Set() implementation
Lodash union vs native Set() implementation
Lodash union vs Native Javascript
Corrected Lodash _.union vs native Set()
lodash union vs native set spread
Comments
Confirm delete:
Do you really want to delete benchmark?