Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash uniqWith vs Set
(version: 1)
Comparing performance of:
Lodash uniqWith 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.21/lodash.min.js'></script>
Script Preparation code:
var array = Array.from(new Array(120), () => Math.floor(Math.random() * 40)) var copy = null
Tests:
Lodash uniqWith
copy = _.uniqWith(array, _.isEqual)
Set
copy = [...new Set(array)]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash uniqWith
Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:130.0) Gecko/20100101 Firefox/130.0
Browser/OS:
Firefox 130 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash uniqWith
146685.6 Ops/sec
Set
547243.2 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined compares two different ways of obtaining unique elements from an array in JavaScript: using the Lodash library's `uniqWith` function and the native `Set` object. ### Options Compared 1. **Lodash `uniqWith` Function** - **Code**: `copy = _.uniqWith(array, _.isEqual)` - **Description**: This test case utilizes the Lodash library to remove duplicate entries from an array. The `uniqWith` method creates a new array by calling the provided comparison function (`_.isEqual` in this case) for each element, allowing for more complex equality checks than simply checking for reference equality. - **Pros**: - Powerful: Handles deep equality checks (e.g., nested objects). - Easy to use: Provides a straightforward API for developers familiar with Lodash. - **Cons**: - Performance: May be slower than native methods for simple equality checks, especially since it performs additional function calls for each comparison. - Dependency: Requires including the Lodash library, which can be significant in terms of payload size if not already in use in the project. 2. **JavaScript `Set` Object** - **Code**: `copy = [...new Set(array)]` - **Description**: This approach uses the native `Set` object, which only allows unique values. By spreading the `Set` back into an array, it effectively filters out duplicates. - **Pros**: - Performance: Generally faster for simple data types (numbers, strings) due to the underlying implementation of `Set`. - Built-in: No external dependencies, making it more lightweight and efficient if no other Lodash features are required. - **Cons**: - Limited to shallow equality: Cannot handle complexities of deep equality checks (e.g., two different object instances with the same properties would be considered different). - Newer JavaScript feature: While widely supported now, older browsers may not support `Set`. ### Other Considerations - **Readability vs. Performance**: While the Lodash approach may be more readable for developers familiar with functional programming paradigms, performance-wise, the native `Set` could be much more efficient for large arrays where deep equality is not required. - **Array Data Types**: The type of data in the array can significantly affect the performance of both methods. The `Set` performs better with primitive types due to its native handling of uniqueness compared to function calls in Lodash. - **Library Use**: If a team is already using Lodash for other purposes, using `uniqWith` might be justifiable despite the extra overhead. For a project without Lodash, the native `Set` method is preferred for its simplicity and reliability. ### Alternatives - **Array.prototype.filter**: You can manually filter duplicates by iterating and checking for existence, though this is less performant than the above methods for large arrays. - **Object key-value pairs**: Another alternative involves using an object to track seen values, which can be equally performant and supports primitive values only. - **Map for complex objects**: For deep comparisons, one could utilize a `Map` to maintain indices on object representations if needing to extend beyond just `Set` or Lodash. In summary, this benchmark provides insight into balancing complexity against performance and dependency management in JavaScript for handling arrays needing uniqueness.
Related benchmarks:
Lodash uniqBy vs Set
Lodash uniqBy vs Set 10000
Lodash uniqBy vs Set with Array of object
Unique lodash vs vanilla
lodash uniq vs set - 3
Lodash uniqBy vs Set vs Set spread
_.uniqWith(arr, _.isEqual).length vs new Set(arr).size 1
lodash uniq vs spread new Set() medium size
New set vs UniqWith
Comments
Confirm delete:
Do you really want to delete benchmark?