Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (large: 10000)
(version: 0)
Comparing performance of:
includes vs lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = new Array(10000).fill(0).map((_, i) => i); var b = new Set(a)
Tests:
includes
return a.includes(9)
lookup
return b.has(9)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
lookup
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
gemma2:9b
, generated one year ago):
This benchmark tests the performance of two methods for checking if an element exists within a collection: `includes()` for arrays and `has()` for Sets in JavaScript. **Options Compared:** * **`includes()` (Array):** Checks if a given value exists within an array. Returns `true` if found, otherwise `false`. * **`has()` (Set):** Checks if a given value exists within a Set. Returns `true` if found, otherwise `false`. **Pros and Cons:** | Method | Pros | Cons | |--------|-------------------------------------------|-----------------------------------------| | includes() | - Familiar to most JavaScript developers<br> - Supports searching within potentially large arrays | - Iterates through the entire array, which can be slow for large arrays | | has() | - Efficient for checking membership in Sets<br> - Guaranteed O(1) time complexity (constant time) | - Requires converting data to a Set, which may introduce overhead if sets are not frequently used | **Other Considerations:** * **Data Structure:** The choice between arrays and Sets depends on your use case. * **Arrays:** Ordered collections that allow duplicates. Suitable for scenarios where order matters or you need to store duplicate values. * **Sets:** Unordered collections that don't allow duplicates. Ideal for situations where uniqueness of elements is crucial, such as storing unique user IDs or removing duplicates from a list. * **Data Size:** For very large datasets, the constant time complexity of `has()` on Sets often leads to significant performance gains over iterating through an array with `includes()`. * **Frequency of Lookup:** If you frequently perform membership checks, using a Set and `has()` can be more efficient than repeatedly searching within an array. **Alternatives:** * **Hash Maps (Object):** While not directly comparable to arrays and Sets, hash maps offer very efficient key-value lookups with O(1) time complexity on average. They might be suitable if you need to store data based on unique keys and perform frequent lookups. Let me know if you have any other questions or would like to explore specific aspects in more detail!
Related benchmarks:
convert to set + set.has vs. array.includes
set.has vs. array.includes 2
set.has vs. array.includes (find 999,999 in 1,000,000)
Array.includes vs Set.has vas Map.has 2
Array.includes vs Set.has vas Map.has with large data set
Comments
Confirm delete:
Do you really want to delete benchmark?