Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash uniqBy vs Set vs Set spread
(version: 0)
Comparing performance of:
Lodash uniqBy vs Set vs Set spread
Created:
3 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>
Script Preparation code:
var MyArr = Array.from({length: 40}, () => Math.floor(Math.random() * 40)); var myCopy = null;
Tests:
Lodash uniqBy
myCopy = _.uniqBy(MyArr);
Set
myCopy = new Set(MyArr)
Set spread
myCopy = [...(new Set(MyArr))]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash uniqBy
Set
Set spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
15 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:150.0) Gecko/20100101 Firefox/150.0
Browser/OS:
Firefox 150 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash uniqBy
1307925.5 Ops/sec
Set
3090095.2 Ops/sec
Set spread
1505813.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation. **Overview** The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The benchmark tests three approaches to removing duplicate elements from an array: using Lodash's `uniqBy` function, creating a new `Set` object, and spreading the `Set` object using the spread operator (`[...]`). **Tested Approaches** 1. **Lodash uniqBy**: This approach uses the Lodash library's `uniqBy` function to remove duplicate elements from the array based on a custom comparison function. 2. **Set**: This approach creates a new `Set` object and adds each element of the original array to it. Since sets only store unique values, this approach removes duplicates automatically. 3. **Set spread**: This approach creates a new `Set` object and spreads its elements into an array using the spread operator (`[...]`). The resulting array will have duplicate elements removed. **Pros and Cons** 1. **Lodash uniqBy**: * Pros: Customizable comparison function allows for fine-grained control over duplicate removal. * Cons: Requires Lodash library to be included, which may add unnecessary overhead. 2. **Set**: * Pros: Fast and efficient, as sets only store unique values. * Cons: Does not preserve the original array order, and may require additional processing to restore it. 3. **Set spread**: * Pros: Preserves the original array order and is relatively fast. * Cons: May be slower than using a `Set` object directly, as it involves creating an intermediate array. **Library: Lodash** Lodash (a library of functions for functional JavaScript programming) provides the `uniqBy` function, which removes duplicate elements from an array based on a custom comparison function. The comparison function is used to determine whether two elements are equal or not. **Special JS Feature/Syntax: Spread Operator ([...] )** The spread operator is used in the "Set spread" approach to create a new array by spreading the elements of the `Set` object. This syntax was introduced in ECMAScript 2015 (ES6) and allows for easy creation of arrays from other iterables, such as sets. **Other Alternatives** 1. **Array.prototype.filter()**: Instead of using a `Set` object or Lodash's `uniqBy` function, you could use the `filter()` method to remove duplicate elements from an array. 2. **Object.values() and Array.from()**: If you need to preserve the original array order, you could use `Object.values()` to get the values of an object (which can be used as a set), followed by `Array.from()` to create an array from it. Here's some sample code using these alternatives: ```javascript // Using filter() var myArr = [...]; // your array var myCopy = myArr.filter((val, index, self) => { return index === self.indexOf(val); }); // Using Object.values() and Array.from() var obj = { /* your object */ }; var myObjSet = new Set(Object.values(obj)); var myCopySpread = [...myObjSet]; ```
Related benchmarks:
Lodash uniqBy vs Set
Lodash uniqBy vs Set 10000
Unique lodash vs vanilla
lodash uniq vs Array.from(new Set()) vs spread new Set() [big arrays 2]
Comments
Confirm delete:
Do you really want to delete benchmark?