Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash _.union vs native Set() vs Concat & filter
(version: 0)
Comparing performance of:
_.union vs Set() vs Set() convert back to array vs Concat & Filter
Created:
7 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] Array.prototype.cfunion = function (array) { var conc = this.concat(array); return conc.filter((i,p)=>{ return conc.indexOf(i) == p }); };
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)
Concat & Filter
var c = a.cfunion(b)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
_.union
Set()
Set() convert back to array
Concat & Filter
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/605.1.15 (KHTML, like Gecko) Version/18.0.1 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.union
4968239.0 Ops/sec
Set()
7146668.5 Ops/sec
Set() convert back to array
4612684.5 Ops/sec
Concat & Filter
8762077.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **Benchmark Overview** The test compares three approaches to create a union of two arrays: 1. `Lodash _.union` 2. `native Set()` (using the built-in `Set` constructor) 3. `Concat & Filter` These three approaches are compared because they represent different ways to solve the same problem: merging two arrays into one without duplicates. **Options Compared** * `_.union`: Uses Lodash's utility function to merge two arrays. * `native Set()`: Utilizes JavaScript's built-in `Set` constructor, which is a collection of unique values. * `Concat & Filter`: Merges two arrays using the `concat()` method and then filters out duplicates using an arrow function. **Pros and Cons** 1. **_.union (Lodash)**: * Pros: Concise and readable code, Lodash's utility functions are well-maintained and widely used. * Cons: Requires an additional library dependency (Lodash). 2. **native Set()**: * Pros: Fast and efficient, as `Set` is implemented in native JavaScript. * Cons: May require some manual effort to convert the resulting set back into an array. 3. **Concat & Filter**: * Pros: No additional library dependencies required, can be easily optimized for performance. * Cons: More verbose code compared to Lodash's `_.union` and may not be as readable. **Library and Syntax** The benchmark uses the Lodash library, which is a popular utility library for JavaScript. It provides a range of functions for tasks like array manipulation, object creation, and more. **Special JS Features or Syntax** There are no special JS features or syntax used in this benchmark that would require prior knowledge of specific language features. However, the use of `Set` as a data structure is a common pattern in JavaScript, so familiarity with basic Set operations may be helpful. **Other Alternatives** If you're interested in exploring alternative approaches, here are some additional options: * Using `Array.from()` to convert a `Set` back into an array: This approach is similar to the `Concat & Filter` method but uses `Array.from()` instead. * Using a custom implementation of the union operation using bitwise operations or other algorithms. * Using a different library or framework that provides optimized implementations for array merging and filtering. These alternatives might offer better performance or readability, depending on your specific use case and requirements.
Related benchmarks:
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?