Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (large: 100)
(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(100).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 compares the performance of two methods for checking if an element exists in a collection: `includes()` on an array and `has()` on a Set. **Options Compared:** * **`a.includes(9)`:** This uses the `.includes()` method on an array (`a`) to check if the value `9` is present in the array. * **`b.has(9)`:** This uses the `.has()` method on a Set (`b`) to check if the value `9` exists within the Set. **Pros and Cons:** * **`.includes()` (Array):** * **Pros:** Simple syntax, commonly used for array lookups. * **Cons:** Can be slower than Sets for large collections, especially when checking for non-existent elements. Arrays perform a linear search through all elements. * **`.has()` (Set):** * **Pros:** Generally faster than `.includes()` on arrays, particularly for large collections. Sets use hash tables internally, enabling near constant time complexity lookups. * **Cons:** Sets only store unique values; duplicates are automatically removed. **Other Considerations:** * **Data Structure Choice:** If you need to ensure uniqueness of elements, a Set is more appropriate. If order matters or you have duplicates, an array is suitable. * **Collection Size:** For small collections, the performance difference might be negligible. However, for large datasets, Sets often provide significant speed improvements. **Alternatives:** * **`indexOf()` (Array):** This method returns the index of the first occurrence of a value in an array. If not found, it returns -1. * Pros: Can return the index if found. * Cons: Still performs a linear search and can be slow for large arrays. * **Custom Hash Table:** You could implement your own hash table using JavaScript objects or other data structures. This offers flexibility but requires more development effort. Let me know if you have any other questions!
Related benchmarks:
convert to set + set.has vs. array.includes
set.has vs. array.includes 2
has vs includes
Includes (array) vs Has (Set)
Array.includes vs Set.has vas Map.has 2
Comments
Confirm delete:
Do you really want to delete benchmark?