Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array immutable union: lodash union vs object.assign vs uniq
(version: 0)
Comparing performance of:
lodash union vs object.assign vs lodash uniq spread
Created:
7 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]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash union
object.assign
lodash uniq spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash union
4313255.0 Ops/sec
object.assign
1307867.0 Ops/sec
lodash uniq spread
6789344.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark that compares the performance of three different approaches for finding unique elements in an array: `_.union` from Lodash, `Object.assign` with spreading syntax, and `_.uniq` with spreading syntax. **Approaches Compared** 1. **Lodash `_.union`**: This approach uses the `_.union` method from Lodash to find unique elements between two arrays. 2. **`Object.assign` with Spreading Syntax**: This approach uses the spread operator (`...`) to concatenate two arrays, which is then assigned to a new array using `Object.assign`. The resulting array contains only unique elements. 3. **Lodash `_.uniq` with Spreading Syntax**: Similar to the previous approach, but uses `_.uniq` instead of `_.union`. **Pros and Cons of Each Approach** 1. **Lodash `_.union`**: * Pros: Easy to read and understand, well-maintained library. * Cons: May have performance overhead due to lazy evaluation. 2. **`Object.assign` with Spreading Syntax**: * Pros: Fast and efficient, can take advantage of browser optimizations. * Cons: Less readable than other approaches, requires understanding of spread operator behavior. 3. **Lodash `_.uniq` with Spreading Syntax**: * Pros: Similar to previous approach, but with better performance due to optimized implementation. * Cons: Still relies on Lodash library. **Library and Purpose** 1. **Lodash**: A popular JavaScript utility library that provides a wide range of functional programming helpers, including `_.union` and `_.uniq`. 2. **Object.assign**: A built-in JavaScript method for assigning properties from one or more sources to an object. **Special JS Feature or Syntax** The test cases use the spread operator (`...`) which is a modern JavaScript feature that allows for spreading elements of arrays into new objects. This syntax was introduced in ECMAScript 2015 (ES6) and has become widely supported by modern browsers. **Other Considerations** * **Browser Support**: The benchmark results are based on Chrome 119 running on Mac OS X 10.15.7, which means the tests may not be representative of other browsers or platforms. * **Code Readability**: While the code is well-structured and readable, the use of Lodash library may make it less accessible to developers who prefer to implement their own utility functions. **Alternative Approaches** 1. **Manual Loop**: A basic approach that uses a manual loop to iterate over both arrays and add unique elements to an output array. 2. **Set-Based Approach**: Another alternative approach that uses sets to find unique elements between two arrays, which can be more efficient than using Lodash library. Overall, the benchmark highlights the importance of considering performance, readability, and maintainability when choosing a JavaScript implementation for common tasks like finding unique elements in an array.
Related benchmarks:
lodash uniq vs native uniq
Array immutable union: lodash union vs flatten and creating a new set
Array immutable union: set from lodash union vs set from lodash flatten
uniqBy vs stringify performance
Comments
Confirm delete:
Do you really want to delete benchmark?