Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array immutable union: lodash union vs flatten and creating a new set
(version: 0)
Comparing performance of:
lodash union vs Set from flatten
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var list = [[ 'a', 'b', 'c' ], [ 'b', 'd', 'a', 'e', 'f' ]];
Tests:
lodash union
var c = _.union(...list);
Set from flatten
var c = new Set(_.flatten(list))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash union
Set from flatten
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash union
3416284.0 Ops/sec
Set from flatten
3536885.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its options. **Benchmark Overview** The benchmark is designed to compare two approaches for performing an immutable union operation on an array of arrays in JavaScript. The test uses Lodash, a popular utility library for JavaScript. **Options Compared** Two options are compared: 1. `lodash union` (via `_union()` method): This approach uses the `_.union()` function from Lodash to perform the union operation. 2. `Set from flatten` (via `new Set() and _.flatten()`): This approach first flattens the array of arrays using `_.flatten()` and then creates a new Set object from the flattened result. **Pros and Cons** 1. `lodash union`: * Pros: More concise and readable code, as it leverages Lodash's optimized implementation. * Cons: May not be as efficient as the alternative approach, depending on the JavaScript engine and optimizations used by Lodash. 2. `Set from flatten`: * Pros: Can be more efficient than the Lodash approach, especially for large datasets, as it avoids the overhead of a function call and uses native JavaScript data structures (Sets). * Cons: Requires additional code to create and manage the Set object, which may make the code slightly less readable. **Lodash Library** Lodash is a utility library that provides a wide range of functions for working with JavaScript arrays, objects, and other data structures. The `_.union()` function is part of this library, providing an efficient implementation of the union operation. **Special JavaScript Feature/Syntax** The benchmark uses the `...` spread operator to pass multiple arrays as separate arguments to the `_union()` function, which is a feature introduced in ECMAScript 2015 (ES6). This allows for concise and expressive code when working with array operations. **Other Alternatives** If you're looking for alternative approaches or alternatives to Lodash, consider the following: 1. `Array.prototype.reduce()`: You can use `reduce()` to perform an accumulator-based union operation on arrays. 2. Manual iteration: You can iterate over the arrays and add elements to a new array or Set object manually, without relying on any library functions. Here's a simple example of using `reduce()` for the `lodash union` equivalent: ```javascript var list = [[ 'a', 'b', 'c' ], [ 'b', 'd', 'a', 'e', 'f' ]; var result = list.reduce((acc, curr) => acc.concat(curr), []); ``` Keep in mind that this approach is less efficient and more verbose than the `lodash union` or `Set from flatten` approaches. If you're looking for a pure JavaScript implementation without relying on Lodash or other libraries, consider using a Set-based approach: ```javascript var list = [[ 'a', 'b', 'c' ], [ 'b', 'd', 'a', 'e', 'f' ]; var set = new Set(); list.forEach(arr => arr.forEach(el => set.add(el))); ``` This approach is more concise but may be slower and less readable than the other alternatives.
Related benchmarks:
Array immutable union: set from lodash union vs set from lodash flatten
Array immutable union: lodash union vs object.assign vs unique
Lodash union vs Native Javascript
lodash union vs native set spread
Comments
Confirm delete:
Do you really want to delete benchmark?