Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash@4.17.21 uniq vs set vs custom unique
(version: 0)
Comparing performance of:
Set vs Array vs Custom function
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js'></script>
Script Preparation code:
function unique(array,) { const result = array.filter( (item, index, array) => array.findIndex((a) => a === item) === index, ); return result.length === array.length ? array : result; }
Tests:
Set
const l = new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]); return l;
Array
const l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return _.uniq(l);
Custom function
const l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return unique(l);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set
Array
Custom function
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
4554063.0 Ops/sec
Array
6861213.0 Ops/sec
Custom function
9313008.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares three approaches to achieve unique values in an array: 1. **Set**: Using a JavaScript built-in `Set` object to remove duplicates. 2. **Array**: Utilizing the Lodash library (`_.uniq`) to remove duplicates from an array. 3. **Custom function**: Implementing a custom function called `unique()` to remove duplicates. **Options Comparison** Here's a brief overview of each approach: * **Set**: This method uses JavaScript's built-in `Set` object, which automatically removes duplicate values. It's fast and efficient but has some limitations: + It only works on arrays with unique values. + It doesn't preserve the original array order. + It can be slower for very large datasets due to memory allocation. * **Array**: This method uses Lodash's `_.uniq` function, which removes duplicate values while preserving the original array order. It's also efficient but has some limitations: + It requires including an external library (Lodash). + It may not perform well on very large datasets due to string comparison overhead. * **Custom function**: This method implements a custom `unique()` function using JavaScript's filtering and indexing capabilities. It's flexible and can work with any array but has some performance limitations: + It requires implementing custom logic, which can be error-prone. + It may not be as efficient as built-in methods like Set or Lodash functions. **Pros and Cons** Here's a summary of the pros and cons for each approach: * **Set**: + Pros: Fast, efficient, and simple to implement. + Cons: Limited to arrays with unique values, doesn't preserve order, and may be slower for large datasets. * **Array**: + Pros: Preserves original array order, flexible, and relatively fast. + Cons: Requires external library (Lodash), may not perform well on very large datasets. * **Custom function**: + Pros: Flexible, can work with any array, and doesn't require an external library. + Cons: Requires implementing custom logic, potentially slower performance. **Library and Special Features** The benchmark uses Lodash's `_.uniq` function, which is a utility function for removing duplicate values from arrays. It's a popular library among JavaScript developers, but it's not included in the standard JavaScript library. There are no special JS features or syntax used in this benchmark that would require specific knowledge of advanced JavaScript concepts. **Other Alternatives** If you want to implement a custom `unique()` function without using Set or Lodash functions, you can use other approaches like: * Using a data structure like a hash table or a Trie. * Implementing a sorting-based approach followed by filtering. * Utilizing the `filter()` and `indexOf()` methods in combination. Keep in mind that these alternatives may have different trade-offs in terms of performance, complexity, and readability.
Related benchmarks:
Lodash uniq vs Set to unique array
lodash uniqBy vs custom uniqBy
Set vs Filter vs _.uniq for unique
set vs lodash uniq vs custom unique 2
Comments
Confirm delete:
Do you really want to delete benchmark?