Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set.has vs Array.includes for small number of items Bv1
(version: 1)
Comparing performance of:
array_1 vs set_1 vs array_8 vs set_8
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const item_1 = Math.floor(Math.random() * 1); const array_1 = new Array(1).map((_, i) => i); const set_1 = new Set(array_1); const item_2 = Math.floor(Math.random() * 2); const array_2 = new Array(1, 2).map((_, i) => i); const set_2 = new Set(array_2); const item_4 = Math.floor(Math.random() * 4); const array_4 = new Array(1, 2, 3, 4).map((_, i) => i); const set_4 = new Set(array_4); const item_8 = Math.floor(Math.random() * 8); const array_8 = new Array(1, 2, 3, 4, 5, 6, 7, 8).map((_, i) => i); const set_8 = new Set(array_8);
Tests:
array_1
array_1.includes(item_1)
set_1
set_1.has(item_1)
array_8
array_8.includes(item_8)
set_8
set_8.has(item_8)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
array_1
set_1
array_8
set_8
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/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array_1
109126928.0 Ops/sec
set_1
130552416.0 Ops/sec
array_8
93259328.0 Ops/sec
set_8
122813024.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark titled "Set.has vs Array.includes for small number of items Bv1" compares the performance of two JavaScript data structures: `Set` and `Array`, specifically focusing on their methods `.has()` and `.includes()`, respectively. The tests are executed on arrays and sets with a small number of items, ranging from 1 to 8. ### Tested Options 1. **Array `includes()` Method**: - **Test Cases**: - `array_1.includes(item_1)` - `array_8.includes(item_8)` - This method checks if a certain element exists within the array and returns a boolean value (`true` or `false`). 2. **Set `has()` Method**: - **Test Cases**: - `set_1.has(item_1)` - `set_8.has(item_8)` - This method checks for the existence of a certain element in a `Set`. Since `Set` only stores unique values, `has()` can be faster than checking in an array, particularly as size increases. ### Performance Results From the benchmark results, we see the execution speeds measured in executions per second for each test: - `set_1`: 130,552,416.0 executions/second - `set_8`: 122,813,024.0 executions/second - `array_1`: 109,126,928.0 executions/second - `array_8`: 93,259,328.0 executions/second ### Pros and Cons of Each Approach - **Array `includes()`**: - **Pros**: - Easy to use and understand. - Suitable for small datasets where performance differences are negligible. - **Cons**: - Performs a linear search, resulting in O(n) time complexity, which becomes significant as the array size increases. - **Set `has()`**: - **Pros**: - Offers O(1) average time complexity due to its hash table implementation. - More efficient for larger collections when checking for existence or membership. - **Cons**: - Slightly more overhead for creating a set than simply using an array. - Only supports unique values, which may not be desirable in some contexts. ### Other Considerations - **Use Case**: When deciding between `Array` and `Set`, it's essential to evaluate the use case. If you need to maintain the order of elements or allow duplicates, an array may be preferable. If you focus only on existence checks of unique values, a set is usually more efficient. - **Memory Overhead**: Sets generally consume more memory than arrays because of the additional data structures used to maintain uniqueness and enable quick lookups. ### Alternatives Other alternatives for checking presence include: 1. **Object**: For key-value pairs, using an object can simulate a set's behavior with similar performance characteristics. However, objects can have prototype properties that may lead to unexpected behaviors unless `Object.create(null)` is used. 2. **Map**: If key-value pairs are required that need to store unique keys (like a set) with associated values, `Map` can be an alternative, with O(1) complexity for lookups akin to `Set`. In summary, this benchmark provides valuable insights into the performance characteristics of using `Set` versus `Array` for membership checks. Depending on the data structure's intended use—such as speed, memory consumption, and uniqueness requirements—software engineers can make an informed decision on which approach to implement.
Related benchmarks:
Array vs object literal v2
Comparing array.concat.apply short form with empty array vs array.flat vs array.reduce
map vs obj vs array
map vs obj vs array 2
array vs set lookup
Object key access vs array index access 10000
my testtestets
Set.has vs Array.includes for small number of items
Set.has vs Array.includes for small number of items Bv2
Comments
Confirm delete:
Do you really want to delete benchmark?