Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash _.union vs _.concat
(version: 1)
Comparing performance of:
_.union vs _.concat
Created:
2 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 = '123456'.repeat(20).split('') var b = '34567'.repeat(20).split('') 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)
_.concat
var c = _.concat(a, b)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.union
_.concat
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.union
482313.4 Ops/sec
_.concat
917955.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark. The provided JSON represents a JavaScript microbenchmark that compares two approaches for creating a union of arrays: `Lodash _.union` and `Lodash _.concat`. **What is tested?** In this benchmark, we have two test cases: 1. `_.union(a, b)`: This test case uses the `_.union()` function from Lodash to create a new array containing all unique elements from both input arrays `a` and `b`. 2. `_.concat(a, b)`: This test case uses the `_.concat()` function from Lodash to concatenate two arrays into one. **Options compared** The two approaches being compared are: * `_.union()`: A function that creates a new array with unique elements from both input arrays. * `_.concat()`: A function that concatenates two arrays into one and then filters out duplicates using the `filter()` method. **Pros and Cons of each approach:** 1. `_.union()`: * Pros: + Creates a new array with unique elements, which can be more memory-efficient than concatenating and filtering. + Can be faster because it avoids the overhead of creating an intermediate array and using `filter()`. * Cons: + May not work correctly for arrays with duplicate values in different positions (e.g., `[1, 2, 2]`). 2. `_.concat()`: * Pros: + Works correctly even if the input arrays have duplicate values. * Cons: + Can be slower because it creates an intermediate array and uses `filter()`, which can lead to extra memory allocations. **Lodash library** The Lodash library is a popular utility library for JavaScript that provides a wide range of functions for working with arrays, objects, and more. The `_.union()` and `_.concat()` functions are part of this library. **Other considerations** * **Use cases**: These two approaches might be used in different scenarios. For example, if you need to create a set-like data structure (e.g., a collection of unique elements), `.union()` might be the better choice. If you just need to concatenate arrays and then remove duplicates, `.concat()` might be sufficient. * **Performance**: The performance difference between these two approaches depends on the specific use case and input data. In general, creating a new array with unique elements can be faster than concatenating and filtering. **Other alternatives** If you're not using Lodash or want to implement these functions yourself, here are some alternative approaches: 1. Use `Set` objects: You can create a set of unique elements by using the `Set` constructor in JavaScript. 2. Implement `.union()` and `.concat()` from scratch: You can write your own implementations of these functions using basic JavaScript features like arrays, loops, and conditional statements. Here's some sample code to get you started: ```javascript // _.union(a, b) implementation function union(a, b) { const result = []; for (const x of a) { if (!result.includes(x)) { result.push(x); } } for (const y of b) { if (!result.includes(y)) { result.push(y); } } return result; } // _.concat(a, b) implementation function concat(a, b) { const result = []; a.forEach((x) => { result.push(x); }); b.forEach((y) => { result.push(y); }); return result; } ```
Related benchmarks:
Array.prototype.concat vs spread operator vs lodash concat
Lodash _.union vs native Set() vs Concat & filter
Array immutable union: lodash union vs flatten and creating a new set
Array.prototype.concat vs spread operator vs lodash.concat - variable and constant
Comments
Confirm delete:
Do you really want to delete benchmark?