Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (tiny)
(version: 1)
Comparing performance of:
includes vs lookup
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = ['abc', 'def']; var b = new Set(a)
Tests:
includes
return a.includes('def')
lookup
return b.has('def')
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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.4 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
201611408.0 Ops/sec
lookup
627682816.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON compares the performance of two methods for checking the existence of an element in a collection: using an array with the `.includes()` method and using a `Set` with the `.has()` method in JavaScript. Below are details on the two approaches, their pros and cons, and other pertinent considerations. ### Approaches Compared 1. **Array.includes()** - **Test Case**: `return a.includes('def')` - **Description**: This method checks if a specific value exists in the array. It returns `true` if the value is found, otherwise it returns `false`. - **Performance Result**: In the benchmark, this test recorded approximately 73,040,552.0 executions per second. 2. **Set.has()** - **Test Case**: `return b.has('def')` - **Description**: The `Set` is a built-in JavaScript object that allows you to store unique values of any type. The `.has()` method checks if a certain element exists in the `Set`, returning `true` if it does, or `false` if it does not. - **Performance Result**: This test recorded approximately 101,673,184.0 executions per second. ### Pros and Cons #### Array.includes() - **Pros**: - Simple to use and understand, as it's a method directly associated with arrays. - Works well for smaller datasets, where performance isn't a critical issue. - **Cons**: - Time complexity is O(n) because it may need to scan the entire array to find the target element, particularly if the target is near the end or not present. - Performance degrades significantly as the size of the array increases. #### Set.has() - **Pros**: - Significantly faster look-up time: the average time complexity is O(1) due to the underlying hash table implementation (though a worst-case scenario could theoretically be O(n) in cases of hash collisions). - Perfect for scenarios involving frequent look-ups, such as membership tests across large datasets or collections. - **Cons**: - Requires the extra overhead of creating a `Set`, which may not be justified for small arrays or collections with few elements. - Limited to unique values; if duplicates are necessary, you'll need to manage that outside of the `Set`. ### Other Considerations - **Use Cases**: - If you have a static or relatively small collection of items where performance is less critical, an array with `.includes()` would suffice. - For larger datasets or scenarios involving frequent membership checks, using a `Set` is highly recommended for better performance. - **Memory Usage**: - While `Set` can use more memory due to its internal structure for fast access, in most scenarios, this trade-off is worth it considering the time efficiency it provides. ### Alternatives - **Other Collections**: Depending on the requirements, you could use objects (for key-value pairs) or Map objects for ordered collections with unique keys, but they may not fit the need for simple existence checks directly like `Set`. - **Libraries**: For additional collection data types or utility functions, libraries like Lodash can help, but for basic existence checks, natively using arrays or sets is usually sufficient. In summary, this benchmark provides valuable insights into the performance differences between using an array's `.includes()` method and a `Set`'s `.has()` method. For high-performance applications, especially with larger datasets, leveraging a `Set` can lead to significant improvements in execution time.
Related benchmarks:
set.has vs. array.includes
set.has vs. array.includes (1m)
set.has vs. array.includes (large)
set.has vs. array.includesdd
set.has vs. array.includes 2333
set.has vs. array.includes (large: 100)
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?