Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array immutable union: underscore union vs vs uniq
(version: 0)
Comparing performance of:
underscore union vs object.assign vs underscore uniq spread
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/underscore@1.12.1/underscore-min.js'></script>
Tests:
underscore 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);
underscore 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
underscore union
object.assign
underscore uniq spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 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
underscore union
1807246.5 Ops/sec
object.assign
1362689.6 Ops/sec
underscore uniq spread
2229462.0 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares three different methods for finding the unique elements in two arrays: **1. `underscore union`**: * **Method**: Uses the `_.union()` function from the Underscore.js library. This library is designed to provide utility functions for working with JavaScript collections, including arrays. * **Pros**: Concise and efficient, leverages a dedicated library optimized for this task. * **Cons**: Relies on an external library which might add overhead if not already included in your project. **2. `object.assign`**: * **Method**: Creates a new array using `Object.assign()` and combines the elements of both input arrays. * **Pros**: Standard JavaScript, no external libraries required. * **Cons**: Less efficient than specialized methods like `_.union()`. Can be harder to read and understand for its specific purpose. **3. `underscore uniq spread`**: * **Method**: Uses the `_.uniq()` function from Underscore.js combined with the spread operator (`...`) to create a new array containing unique elements. * **Pros**: Utilizes a specialized library function, potentially efficient. * **Cons**: Again, relies on an external library. **Other Alternatives:** * **Set Intersection**: JavaScript's `Set` object can be used to efficiently find the union of arrays. Convert each array into a Set, then use the `intersection()` method or logic like: ```javascript const setA = new Set(arrayA); const setB = new Set(arrayB); const uniqueElements = [...new Set([...setA, ...setB])]; ``` * **`filter` and `indexOf`**: You can combine the `filter` method to remove duplicates and the `indexOf` method for checking existence within an array. This is less efficient than the alternatives but demonstrates a purely native JavaScript approach. **Important Considerations:** * **Performance**: The benchmarks show that `underscore uniq spread` and `underscore union` are significantly faster than `object.assign`. For large datasets, performance differences become more noticeable. * **Readability**: While `object.assign` is straightforward, using library functions like `_.union()` can make your code more concise and readable, especially for common tasks like array manipulation. Let me know if you have any other questions!
Related benchmarks:
Lodash uniq vs Set to unique array
lodash uniq vs set - 3
Uniq by vs Array
Lodash union vs Native Javascript
lodash@4.17.21 uniq vs set vs custom unique
Comments
Confirm delete:
Do you really want to delete benchmark?