Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Find unique and duplicate emails
(version: 7)
Comparing performance of:
Reduce vs For Of with has() vs For Of with size
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
emailAddressList = Array.from({length: 60000}, () => `User${Math.floor(Math.random() * 40000)}@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 has()
const uniqueEmailAddressList = new Set(); const suppressedDuplicateUserEmails = new Set(); for (const email of emailAddressList) { if (uniqueEmailAddressList.has(email)) { suppressedDuplicateUserEmails.add(email); } else { uniqueEmailAddressList.add(email); } }
For Of with size
const uniqueEmailAddressList = new Set(); const duplicateEmails = new Set(); let lastUniqueEmailAddressListSize = 0 for (const email of emailAddressList) { uniqueEmailAddressList.add(email); if (lastUniqueEmailAddressListSize === uniqueEmailAddressList.size) { suppressedDuplicateUserEmails.add(email); lastUniqueEmailAddressListSize += 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 has()
For Of 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):
I'll break down the provided benchmark and explain what's being tested, the options compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark is designed to measure the performance of different approaches for finding unique and duplicate emails in an array of 60,000 randomly generated email addresses. The test case uses JavaScript, which allows users to compare various methods with minimal code changes. **Script Preparation Code** The script preparation code generates a list of 60,000 random email addresses: ```javascript emailAddressList = Array.from({length: 60000}, () => `User${Math.floor(Math.random() * 40000)}@testing.com.au`); ``` This list is used as input for the benchmark. **Benchmark Test Cases** There are three test cases: 1. **"Reduce"`** ```javascript const uniqueEmailAddressList = new Set(emailAddressList);\r\n\r\nconst suppressedDuplicateUserEmails = emailAddressList.reduce((list, item, index, array) => {\r\n if (array.indexOf(item, index + 1) !== -1 && list.indexOf(item) === -1) {\r\n list.push(item);\r\n }\r\n return list;\r\n}, []); ``` This approach uses the `reduce` method to iterate through the array and find duplicate emails. 2. **"For Of with has()"** ```javascript for (const email of emailAddressList) {\r\n if (uniqueEmailAddressList.has(email)) {\r\n suppressedDuplicateUserEmails.add(email);\r\n } else {\r\n uniqueEmailAddressList.add(email);\r\n }\r\n} ``` This approach uses a `for...of` loop and the `has` method to check for duplicate emails. 3. **"For Of with size"`** ```javascript let lastUniqueEmailAddressListSize = 0\r\nfor (const email of emailAddressList) {\r\n uniqueEmailAddressList.add(email);\r\n \r\n if (lastUniqueEmailAddressListSize === uniqueEmailAddressList.size) {\r\n suppressedDuplicateUserEmails.add(email);\r\n lastUniqueEmailAddressListSize += 1\r\n }\r\n} ``` This approach uses a `for...of` loop and checks the size of the `uniqueEmailAddressList` to detect duplicate emails. **Comparison** The three approaches differ in their iteration style and use of built-in methods: * **Reduce**: Uses the `reduce` method, which iterates through the array and accumulates results. * **For Of with has()**: Uses a `for...of` loop with the `has` method to check for duplicate emails. * **For Of with size**: Uses a `for...of` loop and checks the size of the `uniqueEmailAddressList`. **Pros and Cons** Here are some pros and cons of each approach: * **Reduce**: + Pros: concise, efficient iteration + Cons: may be less intuitive for developers without experience with reduce() * **For Of with has()**: + Pros: straightforward, easy to understand + Cons: slower due to method call overhead * **For Of with size**: + Pros: simple, easy to implement + Cons: slower due to size comparison overhead **Other Considerations** * The benchmark uses a large input array (60,000 emails) to simulate real-world performance. * The test cases focus on finding unique and duplicate emails, which is a common task in data processing and analysis. * The use of `Set` data structures allows for efficient lookup and insertion of email addresses. **Alternatives** If you're interested in exploring alternative approaches or optimizing the existing methods, consider: * Using other iteration styles (e.g., `forEach`, `map`) * Implementing custom duplicate detection logic * Optimizing memory allocation and garbage collection * Analyzing the benchmark results to identify performance bottlenecks
Related benchmarks:
Methods to remove duplicates from array (test)
Filter vs Set (get unique elements)
Find unique and duplicates
Get unique and duplicate emails
Comments
Confirm delete:
Do you really want to delete benchmark?