Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash _.union vs native Set() v4
(version: 0)
Comparing performance of:
_.union vs Set() vs Set() convert back to array
Created:
3 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) const d = c.includes(a => a===3)
Set()
var c = new Set(a, b) const d = c.has(3)
Set() convert back to array
var c = new Set(a, b)
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):
Let's dive into the world of JavaScript microbenchmarks. **What is tested?** The provided JSON represents a benchmark test between three approaches: 1. **Lodash _.union**: A function from the popular Lodash library that performs union on two arrays. 2. **Native Set() v4**: The built-in `Set` data structure in modern JavaScript engines, which can be used to store unique values. The benchmark tests the performance of these two approaches: * Creating a set from two arrays using `new Set(a, b)` (Test Name: "Set()") * Converting a set back to an array (Test Name: "Set() convert back to array") * Performing union on two arrays using Lodash _.union (Test Name: "_.union") **Options compared** The three options are: 1. **Lodash _.union**: A function that performs union on two arrays. 2. **Native Set() v4**: The built-in `Set` data structure in modern JavaScript engines, which can be used to store unique values. 3. **Converting set back to array**: Converting the result of a set operation back to an array using the spread operator or other methods. **Pros and Cons** Here's a brief summary: * **Lodash _.union**: + Pros: Often faster than native Set() due to optimized implementation, easy to use. + Cons: Requires additional library dependencies, may not be as efficient for very large datasets. * **Native Set() v4**: + Pros: Built-in data structure, no additional library dependencies, can be more efficient for large datasets. + Cons: May have performance issues with small or sparse datasets, less intuitive API compared to _.union. * **Converting set back to array**: + Pros: Can be a good middle ground between Lodash _.union and native Set() v4, allows for easy conversion to other data structures. + Cons: May incur additional overhead due to array creation, may not be as efficient as optimized implementation. **Special JS feature or syntax** None mentioned in the provided JSON. However, it's worth noting that modern JavaScript engines have many features and optimizations that can impact performance, such as: * **Array.prototype.reduce()**: A popular method for performing aggregate operations on arrays. * **Set.prototype.has()**: A simple method for checking if an element exists in a set. **Other alternatives** Some alternative approaches to consider when working with sets and unions: * Using the `Map` data structure instead of `Set`, which can provide faster lookup times. * Utilizing specialized libraries like [lodash-es](https://github.com/lodashjs/lodash-es) or [fast-set](https://github.com/sindresorhus/-fast-set) for optimized set operations. Keep in mind that the best approach often depends on the specific use case and performance 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?