Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array immutable union: lodash union vs ...new Set vs uniq
(version: 0)
Comparing performance of:
lodash union vs new Set vs lodash uniq 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);
new Set
var a = [ 'a', 'b', 'c' ]; var b = [ 'b', 'd', 'a', 'e', 'f' ]; var c = [...new Set([...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
new Set
lodash uniq spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash union
6503471.5 Ops/sec
new Set
4691167.5 Ops/sec
lodash uniq spread
9907050.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Overview** The benchmark is designed to compare the performance of three approaches for removing duplicates from an array: Lodash's `union` function, creating a new Set using the spread operator (`new Set([...a, ...b])`), and Lodash's `uniq` function with the spread operator (`_.uniq([...a, ...b])`). The benchmark aims to determine which approach is the most efficient. **Test Cases** There are three test cases: 1. **Lodash union**: This test case uses Lodash's `union` function to combine two arrays and remove duplicates. 2. **New Set**: This test case creates a new Set by spreading two arrays together, effectively removing duplicates. 3. **Lodash uniq spread**: This test case uses Lodash's `uniq` function with the spread operator to remove duplicates from an array. **Options Compared** The benchmark is comparing three options: 1. Lodash's `union` function 2. Creating a new Set using the spread operator (`new Set([...a, ...b])`) 3. Lodash's `uniq` function with the spread operator (`_.uniq([...a, ...b])`) **Pros and Cons** Here are some pros and cons of each approach: 1. **Lodash union**: * Pros: Easy to use, well-tested library. * Cons: May have a performance overhead due to the complexity of the function. 2. **New Set**: * Pros: Simple, efficient, and widely supported. * Cons: Requires modern JavaScript versions (ECMAScript 2015+) and may not work in older browsers. 3. **Lodash uniq spread**: * Pros: Simple, efficient, and well-tested library. * Cons: May have a performance overhead due to the spread operator. **Libraries** Two libraries are used in this benchmark: 1. Lodash (version 4.17.5): A popular JavaScript utility library that provides various functions for data manipulation and more. 2. No other libraries are used in the test cases. **Special JS Features or Syntax** The benchmark uses modern JavaScript features, such as: 1. Spread operator (`[...a, ...b]`) 2. Rest parameters ( implicit, not explicit) 3. ES6 classes (not used explicitly, but JavaScript version is 2015+, which requires ES6 compatibility) Overall, the benchmark provides a good comparison of three approaches for removing duplicates from an array and highlights the pros and cons of each approach. **Other Alternatives** If you're looking for alternative ways to remove duplicates from an array, here are some options: 1. Use `Array.prototype.filter()`: `a.filter((x, i) => a.indexOf(x) === i)` 2. Use `Array.prototype.reduce()`: `a.reduce((acc, x) => acc.includes(x) ? acc : [...acc, x])` 3. Use `Set` objects: `new Set(a).size` 4. Use `JSON.parse(JSON.stringify([...a, ...b]))`: This approach assumes that the elements in the array are JSON serializable. Keep in mind that each of these alternatives has its own trade-offs and may not be as efficient or easy to use as the approaches tested in this benchmark.
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
lodash uniqBy vs custom uniqBy
Comments
Confirm delete:
Do you really want to delete benchmark?