Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniq vs uniqBy vs Set
(version: 0)
Comparing performance of:
uniq 100 vs uniqBy 100 vs Set 100 vs uniq 1000 vs uniqBy 1000 vs Set 1000 vs uniq 10000 vs uniqBy 10000 vs Set 10000
Created:
3 years ago
by:
Registered User
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:
function createlist(size) { return Array.from(Array(size)).map(() => Math.floor(Math.random() * size)); } var list100 = createlist(100); var list1000 = createlist(1000); var list10000 = createlist(10000); var identity = item => item;
Tests:
uniq 100
var uniq1 = _.uniq(list100);
uniqBy 100
var uniq2 = _.uniqBy(list100, identity);
Set 100
var uniq3 = Array.from(new Set(list100));
uniq 1000
var uniq1 = _.uniq(list1000);
uniqBy 1000
var uniq2 = _.uniqBy(list1000, identity);
Set 1000
var uniq3 = Array.from(new Set(list1000));
uniq 10000
var uniq1 = _.uniq(list10000);
uniqBy 10000
var uniq2 = _.uniqBy(list10000, identity);
Set 10000
var uniq3 = Array.from(new Set(list10000));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (9)
Previous results
Fork
Test case name
Result
uniq 100
uniqBy 100
Set 100
uniq 1000
uniqBy 1000
Set 1000
uniq 10000
uniqBy 10000
Set 10000
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Overview** The provided JSON represents a benchmark suite that tests three different approaches for removing duplicates from an array: `uniq`, `uniqBy`, and `Set`. The test cases involve generating random arrays of varying sizes (100, 1000, and 10,000) and then applying each approach to remove duplicates. **Approaches Compared** 1. **`_.uniq(list)`**: This approach uses the `_.uniq` function from Lodash, which removes duplicate elements from an array while preserving order. 2. **`_.uniqBy(list, identity)`**: This approach uses the `_.uniqBy` function from Lodash, which removes duplicate elements from an array based on a custom equality function (`identity`). 3. **`Array.from(new Set(list))`**: This approach uses the `Set` data structure to remove duplicates from an array. **Pros and Cons of Each Approach** 1. **`.uniq()`**: * Pros: Fast, efficient, and easy to use. * Cons: May not work well for large datasets or datasets with complex equality relationships. 2. **`.uniqBy()`**: * Pros: Allows for flexible sorting and filtering of duplicate elements based on a custom equality function. * Cons: Can be slower than `.uniq()` due to the additional computation required by the `identity` function. 3. **`Array.from(new Set(list))`**: * Pros: Efficient, stable, and easy to understand. * Cons: May not preserve order of elements in case of duplicates. **Other Considerations** * **JavaScript Feature**: This benchmark does not rely on any special JavaScript features or syntax beyond the standard ECMAScript 2020 specification. However, it's worth noting that some older browsers may have issues with the `Set` data structure. * **Lodash Dependencies**: The benchmark relies on Lodash for its utility functions (`_.uniq`, `_uniqBy`). This introduces a dependency on an external library. **Results and Analysis** The provided execution results show that `.uniq()` is generally the fastest approach, followed closely by `Array.from(new Set(list))`. `.uniqBy()` tends to be slower due to the additional computation required by the `identity` function. The performance differences between these approaches are most pronounced for large datasets (e.g., 10,000 elements). Based on these findings, if you need to remove duplicates from an array while preserving order, `.uniq()` is likely the best choice. If you need more flexibility in your duplicate removal process, `_.uniqBy()` may be a better option. For efficiency and simplicity, `Array.from(new Set(list))` is a solid choice. Please note that this analysis assumes that the benchmark is representative of typical use cases and does not account for edge cases or specific requirements that might affect performance.
Related benchmarks:
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
Lodash uniq vs Set to unique array
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn vs custom2
Comments
Confirm delete:
Do you really want to delete benchmark?