Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Member Set vs Array Privilege Check
(version: 1)
Comparing performance of:
Includes Performance vs Has Performance
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var i = 0; var a = [...Array(1000).keys()]; var b = new Set(a);
Tests:
Includes Performance
i = Math.floor(Math.random() * 1000) + 1; return a.includes(i);
Has Performance
i = Math.floor(Math.random() * 1000) + 1; return b.has(i);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Includes Performance
Has Performance
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes Performance
3097987.0 Ops/sec
Has Performance
22002604.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 methods used to check for the existence of a number in a list: using the `Array.prototype.includes()` method and using the `Set.prototype.has()` method. Both approaches offer distinct advantages and disadvantages depending on the use case. ### Explained Benchmark Setup 1. **Preparation Code**: - **Creates an array (`a`)**: The line `var a = [...Array(1000).keys()];` generates an array of integers from 0 to 999. - **Creates a set (`b`)**: The line `var b = new Set(a);` converts the array into a set. Sets in JavaScript store unique values and provide faster lookups compared to arrays. ### Individual Test Cases The benchmark comprises two individual test cases: 1. **Includes Performance**: - **Code**: `i = Math.floor(Math.random() * 1000) + 1; return a.includes(i);` - **What it tests**: This test checks if a randomly generated number `i` (ranging from 1 to 1000) exists in the array `a` using the `includes()` method. - **Pros**: The `includes()` method is straightforward and allows checking for existence of values in arrays, but it performs a linear search. - **Cons**: With a time complexity of O(n), it can become inefficient as the size of the array increases. 2. **Has Performance**: - **Code**: `i = Math.floor(Math.random() * 1000) + 1; return b.has(i);` - **What it tests**: This test checks if the same randomly generated number `i` exists in the set `b` using the `has()` method. - **Pros**: The `has()` method allows for average constant time complexity O(1) due to the underlying hash table implementation of sets, making it significantly faster for lookups in larger data sets. - **Cons**: Using a Set requires additional memory overhead since it stores unique values. ### Benchmark Results Summary The benchmark results reveal the performance of each test case measured in executions per second: - **Has Performance**: 22,002,604 executions per second. - **Includes Performance**: 3,097,987 executions per second. This indicates that using a `Set` for membership checks is vastly more efficient than using an array, particularly as the size of the dataset grows. ### Other Considerations and Alternatives 1. **Memory Usage**: While using a Set is quicker for lookups, it does consume more memory due to its internal structure. If memory usage is a critical factor, using an array might be more suitable for smaller datasets. 2. **Mutability**: Arrays are mutable and can contain duplicate entries. This may be beneficial if the order or duplicates matter for your application. 3. **Other Data Structures**: In environments where additional performance optimizations are required, developers may also consider data structures such as Maps or using libraries like Lodash which provide their own functionalities for efficient search and indexing. 4. **Hybrid Approaches**: Depending on the specific use case, a combination of both data structures may be beneficial. For smaller lists, using an array may suffice, but for larger datasets or performance-intensive applications, sets offer a clear advantage. In summary, the comparison between using `Array.includes()` and `Set.has()` highlights the differences in their performance and use cases, steering developers towards choosing the appropriate method based on their application's requirements.
Related benchmarks:
set.has vs. array.includes 1e5
includes vs has 20220126
Set add vs from array
Some vs. includes TEST1
Array access vs set access
Array access vs set access time
Set vs Array Privilege Check
1000-Member Set vs Array Privilege Check
500 Set vs Array Privilege Check
Comments
Confirm delete:
Do you really want to delete benchmark?