Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash _.union vs native Set() with large arrays
(version: 0)
Comparing performance of:
_.union vs Set() spread into array
Created:
one year 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 = new Array(1000).map((x,i) => i); var b = new Array(500).map((x,i) => 500 - i)
Tests:
_.union
var c = _.union(a, b)
Set() spread into array
var c = [...(new Set(a, b))]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.union
Set() spread into array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.union
60980.3 Ops/sec
Set() spread into array
134689.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of two approaches: `_.union` from Lodash and creating a new Set using the spread operator (`Set()` with large arrays). **Lodash _.union** `.union()` is a function that takes two arrays as input and returns a new array containing all unique elements from both arrays. **Native Set() with large arrays** Creating a new Set using the spread operator `(...)` involves creating an iterable object (in this case, an array) and passing it to the Set constructor. The Set constructor then creates a new Set object containing only unique elements from the input iterable. **Comparison of approaches** The benchmark compares two approaches: 1. **Lodash _.union**: This approach uses the `_.union()` function from Lodash, which is a specialized library for functional programming in JavaScript. 2. **Native Set() with large arrays**: This approach uses the spread operator to create a new Set object from the input array. **Pros and Cons** Here are some pros and cons of each approach: 1. **Lodash _.union** * Pros: + More concise code + May be faster due to optimized implementation * Cons: + Requires an additional library (Lodash) to be included in the benchmark + May not be as performant on older browsers or versions of JavaScript 2. **Native Set() with large arrays** * Pros: + No additional libraries required + Should be supported by all modern browsers and versions of JavaScript * Cons: + Code can become more verbose due to the spread operator syntax + May not be as performant on very large input sizes (due to creation of intermediate arrays) **Other considerations** When choosing between these approaches, consider the following factors: 1. **Code readability**: If conciseness is important, `_.union()` might be a better choice. 2. **Performance**: If performance is critical, native Set() with large arrays might be a better option (although this may not always be the case). 3. **Browser support**: If you need to support older browsers or versions of JavaScript, native Set() with large arrays might be a safer bet. **Library and syntax** The `Set()` constructor is a built-in function in modern JavaScript, introduced in ECMAScript 2015 (ES6). The spread operator (`...`) was also introduced in ES6. In the benchmark code, Lodash library version 4.17.10 is used, which includes the `_union()` function. Overall, this benchmark provides a useful comparison between two approaches to creating unique arrays in JavaScript, highlighting the trade-offs between conciseness, performance, and browser support.
Related benchmarks:
Lodash union vs native Set() implementation
Lodash union vs Native Javascript
Lodash _.union vs native Set() with large arrays using spread
lodash union vs native set spread
Comments
Confirm delete:
Do you really want to delete benchmark?