Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniq vs new Set()
(version: 1)
Comparing performance of:
_.uniq(numbers) vs Array.from(new Set(numbers))
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>
Script Preparation code:
const numbers = [1, 2, 2, 3, 4, 4, 5];
Tests:
_.uniq(numbers)
const uniqueNumbers = _.uniq(numbers);
Array.from(new Set(numbers))
const uniqueNumbers = Array.from(new Set(numbers));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.uniq(numbers)
Array.from(new Set(numbers))
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.uniq(numbers)
29567658.0 Ops/sec
Array.from(new Set(numbers))
11898166.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark you provided compares two different approaches for obtaining unique values from an array of numbers in JavaScript. The specific case in focus is the array `numbers`, which contains duplicate values: `[1, 2, 2, 3, 4, 4, 5]`. Here's a description of the two options being tested, their pros and cons, and some other considerations. ### Options Compared 1. **Using Lodash's `_.uniq` Method:** ```javascript const uniqueNumbers = _.uniq(numbers); ``` 2. **Using Native JavaScript (`new Set()` with `Array.from`):** ```javascript const uniqueNumbers = Array.from(new Set(numbers)); ``` ### Pros and Cons #### 1. **Lodash - `_.uniq(numbers)`** - **Pros:** - **Ease of Use:** Lodash provides a simple and intuitive API. It’s immediately clear that you want to derive unique values from an array using `_.uniq`. - **Performance with Large Datasets:** Lodash is highly optimized for performance in handling arrays and collections. - **Consistency:** Lodash handles edge cases and types consistently, which can be beneficial when working with complex datasets. - **Cons:** - **Dependency:** Using Lodash adds a dependency to the project. This may increase the bundle size if not tree-shaken properly. - **Overhead:** For simple tasks, introducing an external library might introduce unnecessary complexity. #### 2. **Native JavaScript (`Array.from(new Set(numbers))`)** - **Pros:** - **No External Dependencies:** This approach uses native JavaScript features, so there’s no need to include any additional libraries, resulting in a smaller bundle size. - **Modern Syntax:** Using `Set` is a clean and modern approach to achieve uniqueness in arrays. - **Performance:** In environments where native implementations are optimized, this could be very efficient. - **Cons:** - **Complexity:** This approach may be slightly less readable for developers who are not familiar with `Set` and `Array.from`. It involves understanding how sets work and may be less intuitive to novice developers. - **Edge Cases:** If the array includes types that can't be properly compared (e.g., objects), the use of sets may not behave as expected without additional handling. ### Other Considerations - **Performance Results:** From the benchmark results, we see an execution rate of approximately 29.56 million executions per second for `_.uniq(numbers)` compared to about 11.90 million for `Array.from(new Set(numbers))`. This indicates that the Lodash method could be significantly faster in this scenario, especially for larger datasets. - **Browser Compatibility:** Both methods are supported in modern browsers, but npm libraries like Lodash may attract developers who value cross-browser compatibility and consistency over a certain range of environments. ### Alternatives Other alternatives for achieving uniqueness in JavaScript arrays might include: - **Using a Loop:** Implementing a loop with an accumulator to filter out duplicates manually. While this offers granular control, it's generally less efficient and more verbose. - **Using `filter()`:** Combining `filter()` with `indexOf()` can achieve the same result, but it is less performant due to being O(n^2) in complexity. Each of these methods has its own merit depending on the specific use case, dataset size, and required performance. This benchmark comparison highlights the trade-offs between convenience and dependency management versus performance and code simplicity that software engineers should consider in their projects.
Related benchmarks:
_.uniq() vs Set() over large array
lodash uniq vs array from set
Lodash uniq vs Set to unique array
lodash uniq vs set - 3
Uniq Array - lodash uniq vs Array from Set
Lodash unique vs Array.from(new Set)
lodash uniq vs set into array
lodash uniq vs deconstructed set
lodash@4.17.21 uniq vs set vs custom unique
Comments
Confirm delete:
Do you really want to delete benchmark?