Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes but include converting as well
(version: 1)
Comparing performance of:
includes vs lookup
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
Tests:
includes
return a.includes(9)
lookup
var b = new Set(a); return b.has(9)
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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
60132956.0 Ops/sec
lookup
4128059.2 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 for checking if a value exists in a collection: using an array's `includes` method and a `Set`'s `has` method. The benchmark aims to compare the efficiency of these two approaches in terms of execution speed. ### Options Compared 1. **Array's `includes` Method**: - **Benchmark Definition**: `return a.includes(9)` - **Test Name**: `includes` - This method is used to determine whether an array includes a certain value. It functions by iterating through the items in the array, which can result in a linear search. 2. **Set's `has` Method**: - **Benchmark Definition**: `var b = new Set(a); return b.has(9)` - **Test Name**: `lookup` - This method checks whether a specific value exists in a `Set`. A `Set` is a built-in JavaScript object that allows the storage of unique values, enabling quick lookups, typically on average in constant time, as it is implemented using a hash table. ### Pros and Cons #### Array's `includes` Method: - **Pros**: - Simple and straightforward syntax. - Suitable for small arrays or cases where performance is not critical. - **Cons**: - Linear time complexity (O(n)), meaning that performance degrades as the size of the array grows. - May not be efficient for frequent lookups or large datasets. #### Set's `has` Method: - **Pros**: - Constant time complexity on average (O(1)), allowing for much faster lookup times, especially in large datasets. - Automatically manages unique values, eliminating duplicates. - **Cons**: - Slightly more verbose, as it requires creating a `Set` from the array before performing lookups. - Some overhead is incurred from creating the `Set`, especially if the dataset is small or lookups are infrequent. ### Considerations - This benchmark is particularly relevant in scenarios involving frequent membership checks on large datasets. For small arrays or infrequent checks, the overhead of creating a `Set` might not justify its use. - The results from the benchmark indicate a significant performance difference. The `includes` method achieved approximately **41,927,184 executions per second**, while the `has` method resulted in about **3,890,894 executions per second**. This suggests that despite the speed of straightforward usage for `includes`, the performance degradation at scale should be considered. - When using a `Set`, developers should be aware of the initial overhead incurred from creating the `Set` from the array, which could affect performance in cases where this operation occurs frequently or is done in a tight loop. ### Other Alternatives Some alternative approaches to consider include: 1. **Using an Object for Lookup**: Instead of a `Set`, one could use a plain object to store keys and check for existence. This can also provide average constant time complexity for lookups. 2. **Array.prototype.indexOf()**: An older method which can also check for existence but performs similarly to `includes` (linear complexity). 3. **Binary Search on Sorted Arrays**: If the array can be maintained in a sorted state, binary search can be employed for faster lookups (O(log n)), though this requires additional complexity in managing the sorted order with insertions and deletions. Choosing between these options will depend on the specific use case, including the expected size of the dataset and the frequency of membership checks required.
Related benchmarks:
set.has vs. array.includes
set.has vs. array.includes - including extra code for more fair comparison as typically you will have an array and not a set
set.has vs. array.includes on big arrays
set.has vs. array.includes on big arrays s
set.has vs. array.includes (run new Set in has test)
set.has vs. array.includesdd
set.has vs. array.includes first element
set.has vs. array.includes (0)
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?