Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
asdxzcasdqez
(version: 0)
Comparing performance of:
lodash union vs object.assign vs lodash uniq spread vs spread
Created:
4 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>
Tests:
lodash union
var a = [ 'a', 'b', 'c' ]; var b = [ 'b', 'd', 'a', 'e', 'f' ]; var c = _.union(a, b);
object.assign
var a = [ 'a', 'b', 'c' ]; var b = [ 'b', 'd', 'a', 'e', 'f' ]; var c = Object.assign([], a, b);
lodash uniq spread
var a = [ 'a', 'b', 'c' ]; var b = [ 'b', 'd', 'a', 'e', 'f' ]; var c = _.uniq([...a, ...b]);
spread
function arrayUnique(array) { var a = array.concat(); for(var i=0; i<a.length; ++i) { for(var j=i+1; j<a.length; ++j) { if(a[i] === a[j]) a.splice(j--, 1); } } return a; } var array1 = [ 'a', 'b', 'c' ]; var array2 = [ 'b', 'd', 'a', 'e', 'f' ]; // Merges both arrays and gets unique items var array3 = arrayUnique(array1.concat(array2));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash union
object.assign
lodash uniq spread
spread
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 explain what's being tested. **Benchmark Overview** The benchmark consists of four test cases, each measuring the performance of different JavaScript methods to merge and get unique items from two arrays: 1. Lodash `union` method 2. `Object.assign()` method 3. Lodash `uniq()` method with array spread (`...`) 4. A custom implementation of merging and getting unique items using nested loops. **Tested Options** The benchmark compares the performance of three options for merging and getting unique items: 1. **Lodash `union` method**: The `union` method takes two arrays as input and returns a new array containing all unique elements from both arrays. 2. **`Object.assign()` method**: This method creates a new object by copying properties from source objects into the target object. However, in this benchmark, it's used to merge two arrays using the spread operator (`[...a, ...b]`). 3. **Custom implementation using nested loops**: The custom code uses nested loops to iterate through both arrays and remove duplicates. **Pros and Cons of Each Approach** 1. **Lodash `union` method**: * Pros: Fast, efficient, and concise. * Cons: Requires Lodash library, which may not be available or up-to-date in all environments. 2. **`Object.assign()` method**: * Pros: Wide support across browsers and Node.js versions. * Cons: May have performance overhead due to the spread operator (`[...a, ...b]`) creating a new array copy. 3. **Custom implementation using nested loops**: * Pros: Control over algorithm and potential for optimization. * Cons: More complex, slower, and less maintainable than the other two options. **Library and Syntax** The Lodash library is used in test case 1 (`lodash union`) and test case 3 (`lodash uniq spread`). The custom implementation uses nested loops (test case 4). **Other Considerations** * **Browser and Node.js versions**: The benchmark results are specific to Firefox 91 on Ubuntu, which may not be representative of other browsers or environments. * **Device platform**: The benchmark is run on a desktop environment, which might affect the results compared to mobile or web environments. **Alternatives** Other approaches for merging and getting unique items from two arrays include: 1. Using `Set` objects: Create a set from each array and then combine them using the spread operator (`[...new Set(a), ...new Set(b)]`). 2. Utilizing modern JavaScript features like `flat()` and `filter()`: Combine arrays using `flat()` and remove duplicates with `filter()`. 3. Implementing a custom algorithm, such as sorting both arrays and then removing duplicates. These alternatives might offer different performance profiles or trade-offs in terms of complexity and maintainability.
Related benchmarks:
isEmpty vs. vanilla
Lodash vs Ramda fromPairs
Equals vs underscore vs lodash part 2
isUndefined
lodash vs radash 3
Comments
Confirm delete:
Do you really want to delete benchmark?