Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash _.union vs native Set() with spread
(version: 0)
Comparing performance of:
_.union vs Set() convert back to array vs include
Created:
4 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() convert back to array
var c = [...new Set(a, b)]
include
_.forEach(b, (val) => { if (!a.includes(val)) a.push(val); })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.union
Set() convert back to array
include
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 break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares three approaches to achieve similar results: 1. Using Lodash's `_.union()` function 2. Converting a Set data structure back to an array using the spread operator (`[...]`) 3. Implementing an "include" logic manually **Options Compared** The benchmark is testing three options, each with its pros and cons: ### 1. Lodash _.union() * **Pros**: Leveraging a well-tested and optimized library function that handles duplicates efficiently. * **Cons**: Requires including the Lodash library, which may introduce additional overhead. ### 2. Set() convert back to array * **Pros**: Utilizing native JavaScript functionality, which is often faster and more efficient than third-party libraries. * **Cons**: Requires manual handling of potential edge cases (e.g., empty arrays) and can be less readable. ### 3. Manual "include" logic * **Pros**: Provides fine-grained control over the implementation and can be more readable for some use cases. * **Cons**: Can lead to complexity, error-prone code, and slower performance compared to optimized library functions or native implementations. **Library: Set()** The `Set()` data structure is a native JavaScript API that stores unique values. In this benchmark, it's used to create a set from the arrays `a` and `b`, and then converted back to an array using the spread operator (`[...]`). The library is not explicitly mentioned in the JSON provided, but Set() is a widely available and well-documented JavaScript feature. **Special JS Feature/Syntax: Spread Operator** The spread operator (`[...]`) is a relatively recent addition to JavaScript, introduced in ECMAScript 2015 (ES6). It allows creating new arrays by spreading elements from an existing array or other iterable data structure. In this benchmark, the spread operator is used to convert the Set back into an array. **Other Alternatives** If you're interested in exploring alternative approaches, consider: 1. Using `Array.from()` and `Set.prototype.has()`: Another way to create a set from arrays and check for membership. 2. Implementing a custom Set data structure using JavaScript objects or other data structures. 3. Leveraging other libraries or frameworks that provide similar functionality (e.g., `Underscore.js` or `Ramda`). These alternatives might offer trade-offs in terms of performance, readability, or additional dependencies, so it's essential to evaluate them based on your specific use case and requirements. Keep in mind that this benchmark is primarily focused on comparing the performance of these three approaches. If you're interested in exploring more efficient solutions or optimizations, there may be opportunities for further investigation.
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?