Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set versus includes
(version: 1)
set versus includes
Comparing performance of:
Array includes vs set
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = ['SHIP_TO_STORE', 'PICK_FROM_STORE']; var b = new Set(['SHIP_TO_STORE', 'PICK_FROM_STORE']);
Tests:
Array includes
return a.includes('SHIP_TO_STORE');
set
return b.has('SHIP_TO_STORE');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array includes
set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Browser/OS:
Chrome 143 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array includes
143292688.0 Ops/sec
set
127235992.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark testing the performance of two different methods for checking if an element exists in a collection: using an **Array** with the `includes` method versus a **Set** with the `has` method. ### Comparison of Options 1. **Array Includes**: - **Test Case**: `return a.includes('SHIP_TO_STORE');` - **Method**: The `includes` method checks if a specified value exists in the array. It scans the array sequentially to determine if the value is present. - **Pros**: - Easy to understand and use, as it's a native method widely used in JavaScript. - Works well for small arrays. - **Cons**: - Performance can degrade with larger arrays since it has a time complexity of O(n), where n is the number of elements in the array. This means it may have to check each element one by one until it finds a match (or determines the value is not present). 2. **Set Has**: - **Test Case**: `return b.has('SHIP_TO_STORE');` - **Method**: The `has` method checks the presence of a value within a Set. Under the hood, Set uses a hash table for storage, which provides more efficient lookups. - **Pros**: - Offers O(1) average-time complexity for lookups, making it a much more efficient choice for large data sets. - Automatically handles unique values, which can simplify data management when duplicates are not needed. - **Cons**: - Slightly more overhead in terms of memory, since Sets maintain additional data structures. - Requires a little more understanding than using basic arrays, though still well-supported and documented. ### Benchmark Results Overview The benchmark results indicate the performance of both methods when executed in a specific environment (Chrome 134 on Mac OS X 10.15.7): - **Set**: Executions per second: **149,707,888** - **Array includes**: Executions per second: **125,286,384** From these results, it's clear that the Set is the faster option when checking for the existence of an item compared to the Array-based method. ### Other Considerations and Alternatives - While this benchmark focuses solely on these two methods for checking membership, there are other alternatives to consider for different scenarios: - **Objects/Maps**: If you're mapping keys to values and need to check for existence with potentially more data, using an Object or Map might be beneficial. They also provide efficient O(1) lookups similar to Sets. - **Filtered Arrays or find**: If the use case involves more complex conditions than mere existence (e.g., finding a specific object within an array based on properties), employing methods like `Array.prototype.filter()` or `Array.prototype.find()` could be alternatives, albeit at a performance cost depending on implementation. This benchmark effectively illustrates the practical difference between two fundamentally different data structures and their usage in JavaScript, allowing engineers to make more informed decisions based on expected data sizes and performance needs.
Related benchmarks:
set.has vs. array.includes
set.has vs. array.includes (1m)
set vs array
set.has vs. array.includes (large)
set.has vs. array.includes (string input)
Small n set vs array
set.has vs. array.includes first element
set.has vs. array.includes v22
set.has vs. array.includes (0)
Comments
Confirm delete:
Do you really want to delete benchmark?