Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes of IPs
(version: 1)
Comparing performance of:
includes vs lookup
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [ '123.39.218.212', '147.31.131.14', '27.154.247.128', ]; 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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
63018788.0 Ops/sec
lookup
111597576.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON tests the performance of two different approaches for checking the presence of a value in a collection: using `Array.prototype.includes` against `Set.prototype.has`. ### Description of the Options Compared 1. **Array.prototype.includes()**: - **Use Case**: This method checks if an array includes a certain value among its entries, returning `true` or `false` as appropriate. - **Implementation**: The method is called on an array (in this case, `a`) and is designed to be straightforward but less efficient for large datasets, as it needs to check each element linearly until it finds a match or reaches the end. 2. **Set.prototype.has()**: - **Use Case**: This method checks if a value exists in a Set, which is an unordered collection of unique values. - **Implementation**: The method is invoked on a Set (in this case, `b`) and utilizes the optimized lookup capabilities of Set. Because Sets are implemented as hash tables, they typically provide a much faster lookup time, especially for larger datasets. ### Performance Considerations - **Pros of using `Array.includes()`**: - Simplicity: Very easy to read and use. - Familiarity: Widely used and understood by JavaScript developers. - **Cons of using `Array.includes()`**: - Time Complexity: On average, searching for a value in an array has O(n) time complexity, which can become a bottleneck if the array is large. - **Pros of using `Set.has()`**: - Fast Lookups: Offers average-case O(1) time complexity due to the underlying hash table implementation, making it significantly faster for large collections. - Uniqueness: Automatically handles duplicates so only unique values need to be stored. - **Cons of using `Set.has()`**: - Overhead: There is a slight overhead in creation and memory usage as it requires the data to be stored in a Set initially. - Limited Operations: While Sets provide fast lookups, they do not offer as many methods for manipulation compared to arrays (e.g., they do not support operations like slicing). ### Alternative Approaches 1. **Object for Lookup**: - Instead of using an array or a Set, one could use an object to store values as keys with `true` or `false` as the value (e.g., `{ '123.39.218.212': true }`). Lookup would then also be O(1) on average, similar to using a Set. 2. **Using a Map**: - A Map can also be considered, which would provide O(1) lookup similar to a Set but allows more complex values to be stored. 3. **Sorting the Array**: - If the dataset does not change frequently, one could sort the array and use binary search (O(log n)) for lookups, although this would require the overhead of maintaining a sorted state. 4. **For larger datasets**: - Depending on the size and nature of the data, using a more complex structure like a binary search tree or a trie might be appropriate, although this adds complexity. ### Conclusion In summary, this benchmark tests two common approaches for presence checking in JavaScript: `Array.includes()` and `Set.has()`. The results from the latest benchmark indicate that `Set.has()` is significantly faster than `Array.includes()`, further highlighting the efficiency of using Sets for large collections or frequent lookups. Software engineers should choose the approach based on the specific use case, size of the dataset, and performance requirements.
Related benchmarks:
set.has vs. array.includes - including extra code for more fair comparison as typically you will have an array and not a set
set.has vs. array.includes on big arrays
set.has vs. array.includes on big arrays s
set.has vs. array.includes (run new Set in has test)
array.includes vs set.has 2022
set.has vs. array.includes 1000
set.has vs. array.includes 1M
set.has vs. array.includes 2333
set.has vs. array.includes vs array.indexOf (small set of number values)
Comments
Confirm delete:
Do you really want to delete benchmark?