Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Uniq vs Sorted Set
(version: 1)
Uniq returns a soted array removing duplicates. Let's compare with native sort and set.
Comparing performance of:
Uniq vs Set and sort
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:
const myArray = [1, 1, 4, 5, 6, 2, 2, 4, 4, 10, 100, 55, 66, 33, 22, 7]
Tests:
Uniq
return _.uniq(myArray);
Set and sort
return [...new Set(myArray)].sort();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Uniq
Set and sort
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Uniq
2685971.8 Ops/sec
Set and sort
1093619.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON compares two different approaches for generating a sorted array from a set of numbers while removing duplicates. The benchmark focuses on the performance of each approach in terms of executions per second. ### Tested Approaches 1. **Using Lodash's `uniq` method:** - **Implementation:** `return _.uniq(myArray);` - **Description:** This approach utilizes the Lodash library to remove duplicates from `myArray`. The resulting array retains the order of the original inputs but does not sort the output. - **Performance Result:** Executions per second are measured, yielding a result of approximately 2,685,971.75. 2. **Using ES6 `Set` and native `sort`:** - **Implementation:** `return [...new Set(myArray)].sort();` - **Description:** This approach first creates a `Set` from `myArray`, which inherently removes duplicates. The resulting Set is then converted back into an array and sorted. - **Performance Result:** Executions per second are approximately 1,093,619.0. ### Pros and Cons #### Lodash `uniq` Method - **Pros:** - Often easier to use for developers familiar with Lodash, providing a concise way to remove duplicates. - Maintains original order of elements, which can be beneficial depending on the use case. - **Cons:** - May not be as performant as using a `Set`, especially with larger datasets, as the underlying implementation can involve more overhead. - Requires importing an external library (Lodash), which can increase bundle size in web applications. #### `Set` and `sort` - **Pros:** - Performance advantages, especially for larger datasets since `Set` benefits from O(1) time complexity for inserts and checks. - The syntax is more modern and can lead to cleaner code, particularly for developers familiar with ES6 features. - **Cons:** - The order of elements is not preserved when duplicates are removed (the `Set` does not ensure that), which may be undesirable in certain applications. - Requires a second step of sorting, which can affect performance for larger data sets, particularly when duplicates are frequent. ### Other Considerations - **Library Used:** Lodash is widely used in JavaScript development as a utility library that provides a range of functions for common programming tasks, including manipulating arrays, objects, and other data structures. ### Alternatives - Using plain JavaScript without any library, utilizing `filter` and `indexOf` or other similar methods to achieve similar results, though this might lag in performance compared to the other options. - More sophisticated data structures could be implemented if performance is critical, such as using a combination of an array and a hash table to track duplicates more efficiently. - For specific scenarios, developers may also look into functional programming techniques, which can provide different performance characteristics and readability. In conclusion, the benchmark indicates that the Lodash `uniq` method performs significantly better than the `Set` and `sort` approach for the dataset used, although the latter may be more flexible in some scenarios once sorted. Choosing between these options depends on the specific needs of the application, such as performance and order preservation.
Related benchmarks:
lodash uniq vs native uniq
uniqBy performance
uniqBy performance ttt
lodash uniq vs set - 3
lodash uniqBy vs custom uniqBy
uniqBy performance lodash vs native
uniqBy performance and map
Uniq by sorting test
Uniq by sorting test 2
Comments
Confirm delete:
Do you really want to delete benchmark?