Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
500 Set vs Array Privilege Check
(version: 1)
Comparing performance of:
Includes Performance vs Has Performance
Created:
one year ago
by:
Guest
Go to the latest result
Script Preparation code:
var i = 0; var a = [...Array(500).keys()]; var b = new Set(a);
Tests:
Includes Performance
i = Math.floor(Math.random() * 500) + 1; return a.includes(i);
Has Performance
i = Math.floor(Math.random() * 500) + 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
Has Performance
22.5M/s
Includes Performance
5.5M/s
View exact numbers
Test name
Executions per second
✓
Has Performance
22,484,392 Ops/sec
Includes Performance
5,466,240 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
### Benchmark Overview The benchmark titled "500 Set vs Array Privilege Check" compares the performance of two different data structure approaches for checking whether a randomly selected integer from 1 to 500 exists in a collection: an array and a set. The setup involves: - An array (`a`) containing integers from 0 to 499. - A set (`b`) created from the same integers. ### Test Cases #### 1. Includes Performance (Array) - **Benchmark Definition**: `i = Math.floor(Math.random() * 500) + 1; return a.includes(i);` - **Description**: This test case randomly selects an integer `i` and checks if it exists in the array `a` using the `includes()` method. #### 2. Has Performance (Set) - **Benchmark Definition**: `i = Math.floor(Math.random() * 500) + 1; return b.has(i);` - **Description**: Similarly, this test case selects an integer `i`, but checks its existence in the set `b` using the `has()` method. ### Performance Metrics The results indicate the number of executions per second, showcasing how many times each check can be performed in a set time frame: - **Has Performance**: 22,484,392 executions per second - **Includes Performance**: 5,466,240 executions per second ### Pros and Cons of Each Approach #### Array (`includes()` method) - **Pros**: - Simple and intuitive for small collections. - Maintains order of elements. - Supports accessing elements by index comfortably. - **Cons**: - Performance degrades with larger datasets since the `includes()` method checks each element linearly (O(n) time complexity). - Slower lookup for membership tests compared to sets. #### Set (`has()` method) - **Pros**: - Fast lookup time (average O(1)) since set implementations are often backed by hash tables. - Efficient in handling unique values, preventing duplicates automatically. - Overall better performance for membership tests, particularly with larger datasets. - **Cons**: - May consume more memory compared to arrays due to the underlying implementations. - Does not maintain order of elements which may be important for certain applications. ### Other Considerations When deciding between arrays and sets, one must consider the size of the dataset and the operations that will be most frequently performed. If the primary requirement is to check for the presence of items, the set is usually the better choice. ### Alternatives Other alternatives to consider when working with collections in JavaScript could include: - **Maps** for key-value pairs, whose existence checks are also O(1) but are semantically different from sets. - **Objects** which can be used for similar purposes but lack certain functionalities that sets and maps provide, such as guaranteed uniqueness and better performance in membership tests. - Data structures like **binary trees** or **tries** for specific use cases involving sorted data or hierarchal structures, but they come with their own complexity and overhead. In conclusion, the benchmark clearly illustrates the advantages of using a `Set` for membership checks over an `Array`, evidenced by the significant difference in performance metrics provided.
Related benchmarks
Array access vs set access
Array access vs set access time
1000-Member Set vs Array Privilege Check
Member Set vs Array Privilege Check
Comments
Confirm delete:
Do you really want to delete benchmark?