Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash uniqBy vs Set, big array
(version: 1)
Comparing performance of:
Lodash uniqBy vs Set
Created:
10 months 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() * 100000)); 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:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash uniqBy
127.3 Ops/sec
Set
250.4 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 10 months ago):
The benchmark provided is designed to compare the performance of two methods for obtaining unique values from a large array in JavaScript: using the Lodash library’s `uniqBy` function and using the native JavaScript `Set` object. ### Benchmark Breakdown 1. **Preparation Code**: - **Array Creation**: `MyArr` is initialized as a large array (with 100,000 elements) containing random integers from 0 to 99,999. This simulates a substantial dataset for testing. - **Copy Variable**: A variable `myCopy` is declared to hold the result of the uniqueness operation. 2. **Loading External Library**: - The script includes Lodash via a CDN (content delivery network). Lodash is a popular utility library that offers various methods to facilitate JavaScript development, including `uniqBy`, which helps filter out duplicate objects based on specified criteria. 3. **Test Cases**: - **Test Case 1: Lodash `uniqBy`**: ```javascript myCopy = _.uniqBy(MyArr); ``` This line leverages Lodash’s `uniqBy` method to extract unique values from `MyArr`. It is particularly handy for collections of objects where you may want to eliminate duplicates based on specific properties. - **Test Case 2: JavaScript `Set`**: ```javascript myCopy = [...new Set(MyArr)]; ``` This approach uses the `Set` object, which inherently maintains unique values. The spread operator (`...`) converts the `Set` back into an array. ### Results - **Executions per Second**: The benchmark results show that the `Set` approach executed 250.43 times per second compared to 127.29 times per second for the Lodash `uniqBy` method. ### Pros and Cons **Lodash `uniqBy`**: - **Pros**: - Can handle complex cases effectively, such as filtering out duplicates based on specific object attributes. - More readable in contexts requiring dedicated uniqueness logic (e.g., complex objects). - **Cons**: - Slower for large datasets compared to native methods like `Set`. - Requires loading an external library, which can increase the size of your application bundle. **JavaScript `Set`**: - **Pros**: - Generally faster for primitive values due to better native optimizations. - No external library dependencies, resulting in a smaller footprint and improved performance. - **Cons**: - Only works seamlessly with primitive values. For objects, additional logic is necessary to determine uniqueness (e.g., converting objects to strings). - Slightly less readable in contexts where you need to define uniqueness criteria beyond just values themselves. ### Other Considerations - **Other alternatives** include using the `filter` method along with an auxiliary structure or deduplication functions that implement different algorithms. For instance, using `filter` with a lookup object can also achieve deduplication but typically does not outperform `Set` in terms of performance. - For more complex deduplication (e.g., using multiple attributes of an object), developers might need to use combinations of approaches and even return to employing Lodash for its higher-level utility. Overall, for pure performance in working with large arrays of primitive values, using `Set` is advisable. However, for nuanced object collections, the choice between `uniqBy` and other methods (or combinations thereof) will depend on specific project requirements and considerations around code clarity and maintainability.
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 vs map 4
lodash uniq vs set my 2
New set vs UniqWith
Lodash uniqBy vs Set with Big array
Lodash uniqBy vs Set with Big array 24234234
Comments
Confirm delete:
Do you really want to delete benchmark?