Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes counter
(version: 1)
Comparing performance of:
includes vs lookup
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']; var b = new Set(a) var c = 0
Tests:
includes
c += +a.includes(`${(Math.random() * 10) >> 0}`) > 5 ? 1 : -1
lookup
c += +b.has(`${(Math.random() * 10) >> 0}`) > 5 ? 1 : -1
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 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0
Browser/OS:
Firefox 133 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
11994139.0 Ops/sec
lookup
29483812.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark in question compares the performance of two different methods to check for the presence of an element within a collection: using an array's `includes` method and a Set's `has` method. The benchmark examines how quickly each method can determine whether a randomly selected value (between 0 and 9) is present in the respective data structures. ### Options Compared 1. **Array's `includes` Method** - **Test Case**: `c += +a.includes(`${(Math.random() * 10) >> 0}`) > 5 ? 1 : -1` - **Description**: This test checks if a randomly generated number (in string form) is included in the array `a`. The `includes` method returns `true` if the value is present, otherwise `false`. 2. **Set's `has` Method** - **Test Case**: `c += +b.has(`${(Math.random() * 10) >> 0}`) > 5 ? 1 : -1` - **Description**: This test evaluates whether the same random number is present in the Set `b`. The `has` method returns `true` if the value exists in the Set. ### Pros and Cons #### Array Includes - **Pros**: - Familiarity: The `includes` method is a standard way to check for an element in an array which many developers are accustomed to. - Works well for small arrays when performance is less critical. - **Cons**: - Performance: The `includes` method performs a linear search which means its time complexity is O(n). This can be relatively slow, especially for larger arrays. #### Set Has - **Pros**: - Performance: The `has` method in a Set uses a hash table for storage, allowing for average-case constant time complexity O(1) for lookups. - Ideal for checking existence without concern for order or duplicates. - **Cons**: - Overhead: There is some overhead for creating a Set, especially when converting an existing array into a Set. For very small data sets or trivial applications, this overhead might not justify the performance improvement. ### Considerations - **Data Structure Choice**: Choosing between arrays and Sets should depend on the use case. If you frequently need to check for the existence of elements, Sets are typically a better choice due to their faster lookup times. In contrast, if you need to maintain order or perform operations such as mapping or reducing, arrays may be more appropriate. - **Randomness in Tests**: The use of `Math.random()` in the benchmarks attempts to simulate a variety of elements being searched for, which reflects a more realistic scenario of usage patterns. ### Alternative Methods 1. **Object-based Lookup**: An additional approach could involve using a plain object (as a hash map) for very fast lookups, similar to Sets. However, this would not naturally handle non-string types as keys without transformation, which can limit usability. 2. **Typed Arrays**: For numeric types, JavaScript has typed arrays that can be more memory-efficient, but they still do not address the issue of fast existence checks without additional logic. ### Conclusion In general, comparing the two methods, using a Set would likely yield better performance for large datasets due to faster lookups. The benchmark results indicate this with a substantial difference in executions per second, highlighting the efficiency of the Set's `has` method over the array's `includes` in this usage context.
Related benchmarks:
set.has vs. array.includes (40)
Array.includes vs. Object key lookup vs. Set.has
set.has vs. array.includes large213
set.has() vs. array.includes()
Array.includes vs Object key lookup vs Set.has
set.has vs. array.includes 10k el
set.has vs. array.includes (100000)
set.has vs object key lookup vs array includes
array includes vs object key lookup vs Set.has
Comments
Confirm delete:
Do you really want to delete benchmark?