Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Find unique and duplicates
(version: 4)
Comparing performance of:
Reduce vs For Of with size vs ForEach with size
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
emailAddressList = Array.from({length: 10000}, () => `User${Math.floor(Math.random() * 5000)}@testing.com.au`);
Tests:
Reduce
const uniqueEmailAddressList = new Set(emailAddressList); const suppressedDuplicateUserEmails = emailAddressList.reduce((list, item, index, array) => { if (array.indexOf(item, index + 1) !== -1 && list.indexOf(item) === -1) { list.push(item); } return list; }, []);
For Of with size
const uniqueEmailAddressList = new Set(); const duplicateEmails = new Set(); let lastSize = 0 for (const email of emailAddressList) { uniqueEmailAddressList.add(email); if (lastSize === uniqueEmailAddressList.size) { suppressedDuplicateUserEmails.add(email); lastSize += 1 } }
ForEach with size
const uniqueEmailAddressList = new Set(); const duplicateEmails = new Set(); let lastSize = 0 emailAddressList.forEach(email => { uniqueEmailAddressList.add(email); if (lastSize === uniqueEmailAddressList.size) { suppressedDuplicateUserEmails.add(email); lastSize += 1 } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Reduce
For Of with size
ForEach with size
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 measure the performance of finding unique and duplicate email addresses in a large list. The script preparation code generates an array of 10,000 random email addresses, which is then used as input for the test cases. **Test Cases** There are three test cases: 1. **Reduce**: This test case uses the `reduce()` method to find unique and duplicate email addresses. It creates a set of unique email addresses using `Set(emailAddressList)`, and then iterates through the original array, adding each email address to the set only if it's not already present in the set (i.e., it's a duplicate). The resulting set contains both unique and duplicate email addresses. 2. **For Of with size**: This test case uses a `for...of` loop with a `size` property to find unique and duplicate email addresses. It creates two sets, one for unique email addresses (`uniqueEmailAddressList`) and another for duplicate email addresses (`duplicateEmails`). The loop iterates through the original array, adding each email address to the unique set. If the size of the unique set hasn't changed since the last iteration (i.e., no new duplicates were added), it adds the current email address to the duplicate set. 3. **ForEach with size**: This test case is identical to the previous one, but uses the `forEach()` method instead of a `for...of` loop. **Library and Special JS Features** In this benchmark, the following libraries and special JS features are used: * `Set`: A built-in JavaScript object that maintains an internal set of unique values. It's used to find duplicate email addresses. * `reduce()`, `forEach()`, and `for...of` loops: These are standard JavaScript methods for iterating through arrays or iterables. **Options Compared** The three test cases compare the following options: 1. **For Of with size**: This method is efficient because it uses a single loop iteration to check if an email address has been added to the unique set before. 2. **ForEach with size**: Similar to `For Of with size`, but uses the `forEach()` method instead of a `for...of` loop. 3. **Reduce**: This method is less efficient than the two previous options because it creates a new array and iterates through it multiple times, which can lead to more overhead. **Pros and Cons** Here are some pros and cons for each option: 1. **For Of with size**: * Pros: Efficient, uses a single loop iteration. * Cons: Less readable than other options due to the use of `size` property. 2. **ForEach with size**: * Pros: Similar efficiency to `For Of with size`, but might be more readable for some developers. * Cons: Slightly slower due to additional overhead from the `forEach()` method. 3. **Reduce**: * Pros: More concise and readable code, can be used to perform multiple operations in a single loop. * Cons: Less efficient than other options due to additional overhead. **Other Considerations** When choosing an option for this benchmark, consider the trade-off between efficiency and readability. If you prioritize performance, `For Of with size` or `ForEach with size` might be better choices. However, if you prefer a more concise and readable code, `Reduce` could be a good option. As a side note, it's worth noting that this benchmark is primarily designed to test the performance of finding unique and duplicate values in an array, rather than testing other aspects of JavaScript performance or behavior.
Related benchmarks:
Methods to remove duplicates from array (fork)
Methods to remove duplicates from array (test)
Find unique and duplicate emails
Get unique and duplicate emails
Comments
Confirm delete:
Do you really want to delete benchmark?