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:
8 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
llama3.1:latest
, generated one year ago):
Let's dive into the benchmark test case. **What is being tested?** The benchmark tests the performance of three different approaches to merge and deduplicate two arrays (`a` and `b`) in JavaScript: 1. Using the `_.union()` function from the Underscore.js library (also known as Lodash). 2. Using the `Object.assign()` method to concatenate the two arrays. 3. Using the spread operator (`[...array]`) with `_.uniq()` function from the same library. **Options compared** We are comparing three approaches: 1. **Underscore union**: using `_.union(a, b)` from Underscore.js to merge and deduplicate the two arrays. 2. **Object.assign**: using `Object.assign([], a, b)` to concatenate the two arrays and then convert the resulting array to a Set (which is an immutable collection in JavaScript) and back to an array. 3. **Underscore uniq spread**: using `[...a, ...b]` with `_.uniq()` from Underscore.js to merge and deduplicate the two arrays. **Pros and cons of each approach** Here are some pros and cons for each option: 1. **Underscore union**: * Pros: Simple to use, readable code. * Cons: Requires including an additional library (Underscore.js). 2. **Object.assign**: * Pros: Native JavaScript method, no need for external libraries. * Cons: Creates a new array by concatenating the original arrays, which can be inefficient if the arrays are large. Also, it requires converting the resulting array to a Set and back to an array, which adds overhead. 3. **Underscore uniq spread**: * Pros: Also uses native JavaScript features (spread operator), no need for external libraries. * Cons: Requires including the `_.uniq()` function from Underscore.js. **Other considerations** When choosing an approach, consider the following: * If you're already using Underscore.js in your project, it might be worth using their functions to maintain consistency and avoid adding another library. * If you prefer not to use external libraries or have performance concerns with larger arrays, `Object.assign` might be a better choice. However, keep in mind its efficiency limitations. * For most cases, where the arrays are relatively small, all three approaches should yield similar performance. **Library: Underscore.js** Underscore.js is a popular JavaScript library that provides functional programming helpers and utilities. The `_.union()` function merges two or more arrays into one array with no duplicates, while the `_.uniq()` function returns an array of unique elements from any iterable (array, object, etc.). **JavaScript feature: Spread operator** The spread operator (`[...array]`) is a JavaScript syntax sugar that converts an array or other iterable into individual elements. It's used in conjunction with other functions to create new arrays, merge objects, and more. If you're unfamiliar with any of these concepts or features, I'd be happy to explain them further!
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?