Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array vs Set (Large)
(version: 1)
Comparing performance of:
Array vs Set
Created:
6 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.5/lodash.min.js'></script>
Script Preparation code:
var directAddresses = []; for(var i=1; i< 100; i++) { directAddresses.push(`${i}deliverymanage@direct.com`); } var mySetDomains = new Set(); for(var i=1; i< 10000; i++) { mySetDomains.add(`${i}direct.com`); } var usingArrayDomains = []; for(var j=1; j< 10000; j++) { usingArrayDomains.push(`${j}direct.com`); }
Tests:
Array
_.map(directAddresses, (da) => { const domain = da.split('@')[1]; _.includes(usingArrayDomains, domain); });
Set
_.map(directAddresses, (da) => { const domain = da.split('@')[1]; mySetDomains.has(domain); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array
Set
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):
**Overview of the Benchmark** The provided JSON represents a JavaScript benchmarking test case on MeasureThat.net. The test measures the performance difference between using an Array and a Set data structure in JavaScript for a specific use case: checking if an element exists in an array. **What is tested?** In this benchmark, two test cases are compared: 1. **Array**: The first test case uses an Array (`usingArrayDomains`) to store domain names and checks if each domain name exists in the array using `_.includes(usingArrayDomains, domain)`. This approach requires a linear search through the array for each iteration. 2. **Set**: The second test case uses a Set (`mySetDomains`) to store unique domain names and checks if each domain name exists in the set using `mySetDomains.has(domain)`. This approach allows for constant-time lookups since Sets use hash tables. **Options compared** The two approaches are compared in terms of their performance characteristics: * **Array**: Requires a linear search through the array, which has a time complexity of O(n). This means that as the size of the array increases, the search time will also increase. * **Set**: Allows for constant-time lookups since Sets use hash tables, which have an average time complexity of O(1). **Pros and Cons** Here are some pros and cons of each approach: * **Array**: + Pros: Simple to implement, easy to understand. + Cons: Linear search can be slow for large datasets. * **Set**: + Pros: Constant-time lookups, efficient for large datasets. + Cons: Requires more memory to store the Set, may have additional overhead. **Library and purpose** The `lodash` library is used in both test cases. Lodash is a popular JavaScript utility library that provides a set of functional programming helpers, including `_.includes()`. The purpose of using Lodash is to simplify the implementation of the benchmark and make it more concise. **Special JS feature or syntax** There are no special JS features or syntax used in this benchmark other than the use of Arrays and Sets. **Other alternatives** If an alternative approach was needed, here are some options: * **Using a Map**: Instead of using an Array or Set, you could use a Map to store domain names. Maps have constant-time lookups similar to Sets. * **Using a binary search algorithm**: For larger datasets, you could implement a binary search algorithm to find the existence of a domain name in the array. This would have a time complexity of O(log n). Keep in mind that these alternatives may add complexity to the implementation and may not offer significant performance benefits for small datasets. I hope this explanation helps! Let me know if you have any further questions.
Related benchmarks:
Push to array, vs ES6 Spread.
Push vs Spread JavaScript
Array.from vs Spread (1000 numbers)
lodash uniq vs Array.from(new Set()) vs spread new Set() vs for vs for memory optimized 4
lodash uniq vs set spread
Comments
Confirm delete:
Do you really want to delete benchmark?