Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash _.union vs native Set() (accurate)
(version: 0)
Comparing performance of:
_.union vs spread to Set() vs spread to Set() and spread again
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 = [1, 2, 3, 4, 5] var b = [3, 4, 5, 6, 7]
Tests:
_.union
var c = _.union(a, b)
spread to Set()
var c = new Set([...a, ...b])
spread to Set() and spread again
var c = new Set([...a, ...b]) var d = [...c]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.union
spread to Set()
spread to Set() and spread again
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/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.union
3418568.0 Ops/sec
spread to Set()
4079332.5 Ops/sec
spread to Set() and spread again
3878396.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's happening in this benchmark. **What is tested:** The benchmark compares the performance of three approaches: 1. Using Lodash's `_.union()` method to create a union of two arrays (`a` and `b`). 2. Creating a new `Set` object from the spread operator (`...`) applied to both arrays (`a` and `b`). 3. Creating a new `Set` object from the spread operator (`...`) applied to both arrays (`a` and `b`), and then spreading that set again. **Options compared:** * Lodash's `_.union()` method vs. * Native JavaScript `Set` objects created using the spread operator (`...`) * Creating a new `Set` object, spreading it, and then spreading that result again **Pros and cons of each approach:** 1. **Lodash's `_.union()` method:** * Pros: + Can be more convenient for working with arrays in certain situations. + May perform better than native JavaScript methods if optimized by Lodash. * Cons: + Requires including an external library (Lodash) which may add overhead. + May not be as efficient as native JavaScript methods due to the overhead of a library call. 2. **Native JavaScript `Set` objects:** * Pros: + Built-in and optimized for performance by the JavaScript engine. + No external library dependencies. + Can take advantage of native caching mechanisms if used correctly. * Cons: + Requires understanding of spread operator usage and set creation. + May require additional code to convert between array and set representations. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a collection of functions for common tasks, such as working with arrays, objects, and more. In this case, `_.union()` is used to create a union of two arrays, which is what the benchmark is comparing against native JavaScript `Set` objects. **Special JS feature/syntax:** The benchmark uses the spread operator (`...`) which is a relatively recent addition to the JavaScript language (introduced in ECMAScript 2015). The spread operator allows for creating new arrays or sets by copying elements from an existing array or set. This syntax can be convenient but requires understanding of its usage and potential performance implications. **Other alternatives:** If you're interested in exploring alternative approaches, here are a few options: * Using `Array.prototype.reduce()` to create a union of two arrays. * Utilizing the `Map` data structure instead of `Set`, as it provides similar functionality with additional methods for manipulating keys and values. * Implementing custom logic using bitwise operations or other low-level techniques. However, these alternatives may not be as well-optimized or straightforward as the native JavaScript `Set` objects and spread operator usage.
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?