Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash uniqBy vs Set with Big array 24234234
(version: 1)
Comparing performance of:
Lodash uniqBy vs Set
Created:
one year 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: 100000}, () => Math.floor(Math.random() * 40)); var myCopy = null;
Tests:
Lodash uniqBy
myCopy = _.uniqBy(MyArr);
Set
myCopy = new Set(MyArr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash uniqBy
Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash uniqBy
1066.3 Ops/sec
Set
654.9 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark evaluates two methods for creating a unique array from a larger array of random integers using JavaScript: Lodash's `uniqBy` function and the native JavaScript `Set` object. ### Benchmark Overview: 1. **Methods Compared:** - **Lodash `uniqBy`:** `myCopy = _.uniqBy(MyArr);` - **Set:** `myCopy = new Set(MyArr);` ### Method Descriptions: - **Lodash `uniqBy`:** - **Library:** Lodash is a modern JavaScript utility library that provides various functions for common programming tasks, including manipulation of arrays and objects. The `uniqBy` function specifically is used to create a new array with all duplicate elements removed based on a given criterion (in this case, the value itself). - **Pros:** - Well-optimized and performant for various data structures. - Offers more flexibility with additional parameters that can define uniqueness (e.g., custom key extraction). - Easier to read and maintain for complex data structures. - **Cons:** - Requires importing the Lodash library, which can add to the bundle size if not tree-shaken properly. - May perform worse than native options for very large datasets due to additional overhead. - **Set:** - **Description:** The `Set` object is a built-in JavaScript object that allows you to store unique values of any type, whether primitive values or object references. When an array is passed to the `Set` constructor, it automatically removes any duplicates. - **Pros:** - Simplicity: The syntax is straightforward and directly leverages the capabilities of JavaScript. - Native implementation means there is no additional library overhead. - Generally highly performant for handling unique collection scenarios. - **Cons:** - Less flexible since it does not allow for defining custom criteria for uniqueness beyond the value itself. - Inability to directly return an array; a conversion is needed if an array is desired, which may add a slight overhead. ### Benchmark Results: - **Lodash `uniqBy`:** 1066.29 executions per second. - **Set:** 654.89 executions per second. The results indicate that `Lodash uniqBy` outperforms the `Set` approach in this particular benchmark, likely due to the optimizations that Lodash has for handling larger data sets and unique criterion extraction. ### Other Considerations and Alternatives: - **Performance Variability:** The performance can vary depending on the array's size, structure, and distribution of values. If you're dealing with a small array, it’s possible that the overhead of Lodash may not yield significant performance benefits. - **Other Alternatives:** - Native `Array.prototype.filter()`: This method can also be used to create unique arrays but typically does not perform as well as `Set` or `Lodash uniqBy` for large datasets. - Manual implementations: Writing custom functions that utilize objects or maps for tracking seen values. - Using other third-party libraries that might provide similar functionality optimized for particular use cases. In conclusion, developers should weigh the pros and cons of using `Lodash uniqBy` versus `Set`, considering factors such as the size of the data, performance requirements, and whether additional flexibility is necessary.
Related benchmarks:
Lodash uniqBy vs Set
Lodash uniqBy vs Set 10000
Lodash uniqBy vs Set with Array of object
Lodash uniqBy vs Set vs Set spread
lodash uniq vs set my 2
New set vs UniqWith
New set vs UniqWith v3
Lodash uniqWith vs Set
Lodash uniqBy vs Set with Big array
Comments
Confirm delete:
Do you really want to delete benchmark?