Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set has vs array includes (1k items)
(version: 1)
Comparing performance of:
array vs set
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
window.__theArr = Array.from({length: 10_000}, (_, el) => el) window.__theSet = new Set(window.__theArr)
Tests:
array
window.__theArr.includes(500);
set
window.__theSet.has(500)
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):
Let's break down the provided benchmarking scenario. **Overview** The test measures the performance difference between using `Array.includes()` and `Set.has()` methods on a large array of 10,000 unique elements. The script preparation code generates an array (`window.__theArr`) and a set (`window.__theSet`) from it, which are then used in individual test cases. **Options Compared** Two options are compared: 1. **Array.includes()**: This method checks if a specified value (in this case, 500) exists in the array. 2. **Set.has()**: This method checks if a specified value (in this case, 500) exists in the set. **Pros and Cons of Each Approach** * **Array.includes():** * Pros: * Widely supported and well-maintained. * Fast and efficient for smaller arrays or when searching for a specific element. * Cons: * Less efficient than Set.has() for large datasets, as it requires iterating through the array to find the element. * **Set.has():** * Pros: * Much faster and more efficient than Array.includes() for large datasets, thanks to set operations' optimized internal implementation. * Can be used for membership testing in sets or maps efficiently. **Library Used: Set** The `Set` library is a built-in JavaScript object that stores unique values. It's implemented as an abstract hash table and provides constant-time complexity for membership testing (has() and size) and insertion/deletion operations. **Other Considerations** * **Data Distribution:** The test uses a uniform distribution of 10,000 unique elements in the array and set, which is unlikely to be representative of real-world data distributions. * **Browser Variability:** The benchmark results may vary depending on the browser version, device platform, operating system, and other factors. **Alternatives** Other alternatives for membership testing include: 1. **Map has():** For objects (maps) that are iterables, `has()` provides a more efficient method than `includes()` or `Set.has()`. 2. **Array.prototype.some():** This method is slower than Array.includes() but can be useful when searching for an element within a subset of the array. 3. **Other Set Operations:** For more complex set operations, consider using methods like `size`, `keys()`, or `values()`. In conclusion, the test measures performance differences between two widely used membership testing methods: Array.includes() and Set.has(). While Set.has() is generally faster for large datasets, it's essential to consider factors like data distribution, browser variability, and alternative approaches when optimizing membership testing in your codebase.
Related benchmarks:
set vs some
set vs some 1000000
set vs array add 1k items
array.includes vs. set.has on the fly
Comments
Confirm delete:
Do you really want to delete benchmark?