Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniq vs [new Set()]
(version: 1)
Comparing performance of:
_.uniq(arrayOfOptions) vs [...new Set(arrayOfOptions)]
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 arrayOfOptions = [1, 2, 3, 7, 2, 4, 5, 6, 7]
Tests:
_.uniq(arrayOfOptions)
_.uniq(arrayOfOptions)
[...new Set(arrayOfOptions)]
[...new Set(arrayOfOptions)]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.uniq(arrayOfOptions)
[...new Set(arrayOfOptions)]
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Browser/OS:
Chrome 143 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.uniq(arrayOfOptions)
21004104.0 Ops/sec
[...new Set(arrayOfOptions)]
9295818.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark titled "uniq vs [new Set()]" compares two approaches for removing duplicate values from an array in JavaScript. The two methodologies being evaluated are: 1. **Lodash's `_.uniq(arrayOfOptions)`**: This is a function from the Lodash library, which is a popular utility library providing many helpful functions for everyday JavaScript programming. The `_.uniq` method creates a new array with duplicate values removed from the given array, maintaining the order of the original elements. **Pros**: - **Easy to Use**: The Lodash API is straightforward and integrates seamlessly with its other utility functions. - **Familiarity**: Many developers are already acquainted with Lodash, which may lead to fewer errors and clearer code. **Cons**: - **Performance Overhead**: Because Lodash is a library, invoking its functions may introduce additional overhead compared to native JavaScript solutions. - **Dependency**: Relying on Lodash means your code must include the library, which can increase bundle size if not properly managed (e.g., if using a bundler that includes unused functions). 2. **Using `new Set(arrayOfOptions)`**: This approach leverages the native JavaScript `Set` object, which is designed to hold unique values. By converting the `Set` back into an array with the spread operator (`...`), you effectively remove duplicates. **Pros**: - **Performance**: Typically, using native data structures like `Set` can be faster than library functions because they are optimized by the JavaScript engine. - **No Dependency**: This method does not require any external libraries, reducing the footprint of your JavaScript code. **Cons**: - **Limited Compatibility**: While modern browsers support the `Set` object, older environments (especially outdated browsers) may not. - **Order Considerations**: Although `Set` maintains insertion order, this behavior is not guaranteed in all scenarios with older JavaScript implementations. ### Benchmark Results: - The benchmarks reveal that `_.uniq(arrayOfOptions)` achieved a performance rate of approximately 21,594,292 executions per second, while `[...new Set(arrayOfOptions)]` reached about 9,884,183 executions per second. This indicates that Lodash's method was significantly faster in this particular context, although performance can vary based on array size and environment. ### Other Considerations: - When performing these types of operations, it’s essential to consider not only raw performance but also readability and maintainability of the code. In scenarios where performance is critical and if you need to manage larger datasets or execute such operations frequently, the choice of method could dramatically affect application performance. - Alternatives to these two approaches include: - **Using traditional loops** to filter duplicates, which can be more verbose but allows for custom logic. - **Using `Array.prototype.filter()`** in conjunction with `indexOf()` or similar methods, though these might be less efficient for larger arrays. In conclusion, the choice between using Lodash's `_.uniq` and the native `new Set()` approach can depend on the needs of the project, including performance considerations, code readability, and the context of use.
Related benchmarks:
_.uniq() vs Set()
_.uniq() vs Set() over large array
lodash uniq vs set updated lodash
lodash uniq vs set - 3
lodash uniq vs set performance
lodash uniq vs set my
lodash uniq vs set my 2
lodash@4.17.21 uniq vs set vs custom unique
uniq vs new Set()
Comments
Confirm delete:
Do you really want to delete benchmark?