Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unique Array [Set vs. Generator]
(version: 0)
Comparing performance of:
Set vs Generator
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function* _unique(iterable, ) { const cache = new Set(); for (const item of iterable) { if (!cache.has(item)) { cache.add(item); yield item; } } }
Tests:
Set
const array = Array.from('YYYY-MM-DD[T]HH:mm:ss.SSSZ'); [...new Set(array)];
Generator
const array = Array.from('YYYY-MM-DD[T]HH:mm:ss.SSSZ'); [..._unique(array)];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
Generator
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 break down the provided benchmark definition and test cases. **Benchmark Definition:** The benchmark is designed to compare two approaches for creating an array of unique elements from a given string: 1. Using `Array.from()` and `[...new Set()]` (Set approach) 2. Using a generator function `_unique()` that yields unique elements (Generator approach) **Script Preparation Code:** ```javascript function* _unique(iterable, ) { const cache = new Set(); for (const item of iterable) { if (!cache.has(item)) { cache.add(item); yield item; } } } ``` This generator function takes an iterable as input and uses a `Set` to keep track of unique elements. It iterates over the input iterable, adds each element to the set if it's not already present, and yields the element. **Html Preparation Code:** There is no HTML preparation code provided for this benchmark. **Individual Test Cases:** The benchmark has two test cases: 1. **Set**: Creates an array of unique elements using `Array.from()` and `[...new Set()]`. ```javascript const array = Array.from('YYYY-MM-DD[T]HH:mm:ss.SSSZ'); [...new Set(array)]; ``` 2. **Generator**: Uses the `_unique()` generator function to create an array of unique elements. ```javascript const array = Array.from('YYYY-MM-DD[T]HH:mm:ss.SSSZ'); [_unique(array)].map(item => item); ``` **Pros and Cons:** 1. **Set approach**: * Pros: + Fast and efficient, as it leverages the optimized implementation of `Set`. + Easy to read and understand. * Cons: + Requires creating an intermediate array using `Array.from()`, which can be memory-intensive for large inputs. 2. **Generator approach**: * Pros: + Memory-efficient, as it uses a generator function that yields values on-the-fly without storing them in memory. + Can handle large inputs without significant memory allocation. * Cons: + Less intuitive and less readable than the Set approach. + Requires understanding of generator functions. **Library:** None **Special JS feature or syntax:** None (although using a generator function is a common JavaScript pattern) **Other Alternatives:** 1. Using `Array.from()` with a custom callback function that filters out duplicates could be another alternative, but it would not provide the same efficiency as the Set approach. 2. Using a library like Lodash's `uniq` function or Ramda's `uniqueBy` function could also be used to create an array of unique elements. However, these libraries would add overhead and are not as efficient as the built-in `Set` implementation. Overall, the benchmark provides a good comparison between two approaches for creating an array of unique elements in JavaScript, highlighting the trade-offs between memory efficiency and readability.
Related benchmarks:
every vs set create 100000
Lodash uniq vs Set to unique array
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn vs custom2
Set vs Filter vs _.uniq for unique
lodash@4.17.21 uniq vs set vs custom unique
Comments
Confirm delete:
Do you really want to delete benchmark?