Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash _.union vs native Set() 2
(version: 0)
Comparing performance of:
_.union vs Set() vs Set() convert back to array
Created:
6 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, 4, 5, 2, 7, 9, 10, 11, 15, 17, 3, 16, 23] var b = [3, 4, 5, 6, 7, 10, 23, 5049, 194, 39 ]
Tests:
_.union
var c = _.union(a, b)
Set()
var c = new Set(a, b)
Set() convert back to array
var c = new Set(a, b) var d = Array.from(c)
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 break down the provided benchmark and its results. **What is tested on the provided JSON?** The provided JSON represents three microbenchmarks that compare the performance of different approaches for creating a union of two arrays in JavaScript: 1. `_.union` from Lodash library 2. Native `Set()` data structure (without converting it back to an array) 3. Native `Set()` data structure with conversion back to an array using `Array.from()` **Options compared** The three options are compared as follows: * `_.union`: A function provided by the Lodash library that creates a new set-like object with all elements from both arrays. * Native `Set()`: A built-in JavaScript data structure that can be used to create a set of unique values. * Native `Set()` + `Array.from()`: Converting the resulting set back to an array using the `Array.from()` method. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. `_.union` from Lodash library: * Pros: Easy to use, provides a convenient function for creating sets. * Cons: Adds overhead due to the use of an external library. 2. Native `Set()`: Fastest option (without conversion) * Pros: Built-in JavaScript data structure, fast performance. * Cons: Requires manual handling of duplicate values. 3. Native `Set()` + `Array.from()`: Slightly slower than native `Set()`, but eliminates manual handling of duplicates. * Pros: Eliminates need for manual handling of duplicates. * Cons: Adds slight overhead due to the use of `Array.from()`. **Library and its purpose** The Lodash library provides a range of utility functions, including `_union`, which can be useful when working with arrays or sets in JavaScript. The library's purpose is to provide a set of reusable functions that can simplify code and improve performance. **Special JS feature or syntax** None mentioned in the provided benchmark JSON. **Other alternatives** If you prefer not to use a library like Lodash, you could consider implementing your own union function using native JavaScript data structures, such as `Set()` and `Array.prototype.forEach()`. However, this approach would likely be slower than using `_.union` from Lodash or the native `Set()` + `Array.from()` approach. In summary, the provided benchmark compares three approaches for creating a union of two arrays in JavaScript: Lodash's `_union` function, native `Set()` data structure, and native `Set()` with conversion back to an array using `Array.from()`. The results show that native `Set()` is generally the fastest option, followed by the native `Set()` + `Array.from()` approach.
Related benchmarks:
_.union vs native Set() implementation
Lodash union vs native Set() implementation
Corrected Lodash _.union vs native Set()
lodash union vs native set spread
Comments
Confirm delete:
Do you really want to delete benchmark?