Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash _.union vs the native Set() for multiple arrays
(version: 1)
Comparing performance of:
_.union vs Set()
Created:
one year 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 = []; var b = []; var c = []; for (i = 0; i < 1000; i++) { a.push(getRandom()); b.push(getRandom()); c.push(getRandom()); a.sort(); b.sort(); c.sort(); } function getRandom() { const minimum = 1; const maximum = 1000; var randomnumber = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; return randomnumber; }
Tests:
_.union
var r = _.union(a, b, c)
Set()
var r = Array.from(new Set([...a, ...b])); r = Array.from(new Set([...r, ...c]));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.union
Set()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.union
10218.9 Ops/sec
Set()
8685.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark. **What is being tested?** The provided JSON represents two JavaScript microbenchmarks: `_.union` from Lodash and `Array.from(new Set([...a, ...b]))`. Both tests aim to measure the performance of combining multiple arrays using a union operation. The test creates three large arrays (`a`, `b`, and `c`) filled with random integers, sorts each array, and then applies the union operation. **Comparison options:** 1. **Lodash _.union**: This approach uses the Lodash library's `_` utility function to perform the union operation. 2. **Native Set()**: This approach uses JavaScript's built-in `Set()` constructor to create a new set from the combined array and then converts it back to an array using `Array.from()`. **Pros and cons of each approach:** 1. **Lodash _.union** * Pros: + Easy to use, as it's a well-established utility function. + Likely to be optimized for performance, given its widespread usage. * Cons: + Adds external dependency (the Lodash library), which might not be desirable in all scenarios. + May have overhead due to the function call and object creation. 2. **Native Set()** * Pros: + No external dependencies, reducing overhead. + Utilizes native JavaScript optimization, potentially leading to better performance. * Cons: + Requires a good understanding of set operations and array manipulation. + Might be slower for very large arrays due to the additional memory allocation. **Library: Set()** The `Set()` constructor is a built-in JavaScript function that creates a new set from an iterable (in this case, an array). A set is a collection of unique values, which makes it ideal for removing duplicates. By using `Array.from(new Set([...a, ...b]))`, the test creates a new set from the combined array and then converts it back to an array using `Array.from()`. This approach has the benefits mentioned earlier (no external dependency) but requires more manual manipulation of arrays. **Special JS feature:** There is no specific JavaScript feature or syntax being tested in this benchmark. The focus is solely on comparing two different approaches for performing a union operation. **Other alternatives:** For larger datasets, you might consider using other data structures like `Map()` or `PrioritizedQueue()`, which could offer optimized performance for certain use cases. Additionally, if the arrays are very large and memory-efficient operations are crucial, you might look into libraries like `array-union` or ` array-difference` that provide specialized functions for these operations. Keep in mind that the choice of approach often depends on the specific requirements of your project, such as performance, memory usage, and ease of maintenance.
Related benchmarks:
Lodash sortBy predicate vs array.prototype.sort n=1000000
Lodash sort vs array.prototype.sort larger set
Lodash sort vs array.prototype.sort (Lodash 4.17.15)
Lodash sort vs array.prototype.sort 3
Lodash sort vs array.prototype.sort fork for spread
Comments
Confirm delete:
Do you really want to delete benchmark?