Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash _.union vs native Set()
(version: 1)
Comparing performance of:
_.union vs Set() vs Set() convert back to array
Created:
8 years ago
by:
Registered User
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 = Array.from(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:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0.1 Safari/605.1.15
Browser/OS:
Safari 26 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.union
8511797.0 Ops/sec
Set()
12974650.0 Ops/sec
Set() convert back to array
8639212.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **What is being tested?** The benchmark compares the performance of two approaches to perform set operations: 1. `Lodash _.union`: This uses the `_.union()` function from the Lodash library, which merges two arrays into a new array with unique elements. 2. `Native Set()`: This uses the built-in `Set` data structure in JavaScript to merge two arrays. **Options compared:** * `Lodash _.union` * `Set()` * `Set() convert back to array` **Pros and Cons of each approach:** 1. **Lodash _.union** * Pros: + Easy to use with existing Lodash library. + Can handle more complex data structures, such as objects or arrays with duplicate values. * Cons: + Adds extra overhead due to the library's complexity and indirection. + May not be optimized for performance, especially on large datasets. 2. **Native Set()** * Pros: + Fast and efficient, using native JavaScript functions. + No additional overhead or dependencies required. * Cons: + Requires manual conversion between arrays and sets (e.g., `Array.from(c)`). + May not be suitable for handling complex data structures. **Library and its purpose:** The Lodash library is a collection of utility functions, including the `_.union()` function used in this benchmark. Its primary purpose is to provide a set of reusable functions for common programming tasks. **Special JS feature or syntax:** None mentioned in this benchmark, but it's worth noting that JavaScript has other features and syntax that may impact performance, such as: * Array methods like `filter()`, `map()`, or `reduce()` might be used instead of `_.union()`. * The use of `for...of` loops or other iteration constructs might be preferred over the Lodash library. **Alternatives:** If you want to compare other set operations, you could consider using: 1. `Array.prototype.reduce()`: Merging two arrays into a new array with unique elements. 2. `Array.prototype.concat()`: Combining two arrays into a single array (not ideal for finding duplicates). 3. Other libraries or modules that provide similar functionality to Lodash. Keep in mind that the specific alternatives and their performance will depend on your use case and requirements.
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?