Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes Performance
(version: 0)
set.has vs. array.includes Performance
Comparing performance of:
includes vs lookup
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array.from({ length: 100000 }, (_, el) => el); 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 on MeasureThat.net compares the performance of two methods for checking if an element exists within a data structure: **1. `array.includes()`:** This built-in JavaScript method checks if a given value exists within an array and returns true if found, false otherwise. **2. `set.has()`:** This method is used on JavaScript Sets, which are collections of unique values. It efficiently determines if a specific value is present in the Set. **Options Compared:** - **`array.includes()`**: This approach utilizes a linear search algorithm to traverse the array, comparing each element with the target value. Its performance degrades as the array size increases. - **`set.has()`**: Sets leverage a more efficient underlying data structure (often a hash table) that allows for near constant-time lookups (O(1) on average). **Pros and Cons:** * **`array.includes()`:** * **Pros**: Simple to implement, readily available in most JavaScript environments. * **Cons**: Performance degrades with larger arrays due to linear search complexity. * **`set.has()`:** * **Pros**: Significantly faster for lookups, especially on large datasets. * **Cons**: Requires an additional step to create and populate the Set object. **Other Considerations:** - **Data Structure Suitability:** If you need a collection with guaranteed uniqueness and efficient membership testing, Sets are highly suitable. If your data doesn't require uniqueness or has limited size, arrays might be simpler. - **Trade-offs:** The performance difference between `includes()` and `has()` becomes more pronounced as the dataset size grows. **Alternatives:** - **Using a hash map (e.g., using `Object`):** You could create an object where keys are the array elements, and values are placeholders. Lookups would then involve checking if the key exists in the object. This can be efficient but might be more complex to implement. - **Specialized data structures:** For very specific use cases or performance-critical scenarios, you might explore more advanced data structures like tries or Bloom filters, which offer specialized efficiency for certain operations. Let me know if you have any further questions!
Related benchmarks:
convert to set + set.has vs. array.includes
set.has vs. array.includes 2
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
3set vs array iteration New doge333
Comments
Confirm delete:
Do you really want to delete benchmark?