Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.includes vs set.has on 100 elements
(version: 1)
100 elements and each element check once
Comparing performance of:
array.includes vs set.has
Created:
10 months ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const arr = []; const elementsCount = 100; for (let i = 0; i < elementsCount; i++) { arr.push(i); }; const set = new Set(arr);
Tests:
array.includes
for (let i = 0; i < elementsCount; i++) { arr.includes(i); };
set.has
for (let i = 0; i < elementsCount; i++) { set.has(i); };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array.includes
set.has
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; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.includes
172310.4 Ops/sec
set.has
19907282.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 10 months ago):
The benchmark described tests the performance of two different approaches for checking the existence of elements in a collection: using the `Array.prototype.includes` method compared to using the `Set.prototype.has` method. Both methods are utilized on a collection consisting of 100 elements, and each element is checked once. ### Benchmark Details **1. Options Being Compared:** - **`array.includes`**: This method is called on an array, and it checks if a specified value is present in the array. - **`set.has`**: This method is called on a Set, a data structure that stores unique values, and it checks if a specified value exists in the Set. **2. Pros and Cons of Each Approach:** - **`array.includes`**: - **Pros**: - Simplicity: Easy to understand and use when working with arrays directly. - Well-integrated with array methods, making it a natural choice for list-like structures. - **Cons**: - Performance: The method has to iterate through the array to check for the presence of the element, leading to a time complexity of O(n) in the worst case (where n is the number of elements in the array). - **`set.has`**: - **Pros**: - Performance: Checking for the presence of an element in a Set is optimized; it generally has a time complexity of O(1) in average scenarios, due to its underlying hash-based structure. - Uniqueness: Automatically enforces unique values, eliminating the need to worry about duplicates. - **Cons**: - Overhead: There is a slight overhead in creating a Set, especially if you're converting an existing array into a Set. - Not suitable for ordered collections if the order of elements is important, as Sets are inherently unordered. ### Other Considerations - **Memory Usage**: A Set may use more memory than an array since it has to keep track of unique elements independently. - **Use Cases**: `array.includes` is beneficial if the order of elements and array-specific operations are necessary. In contrast, `set.has` is a preferred solution when the primary requirement is fast membership checking without concern for element order or duplicates. ### Benchmark Results In the provided benchmark results, we observe that the `set.has` method executed significantly more times per second (approximately 4.55 million) compared to the `array.includes` method (approximately 694 thousand). This reflects the efficiency advantage of using Sets for membership checking. ### Alternatives Other alternatives for checking membership include: - **Using a Map**: Similar to Sets, Maps can also provide O(1) average time complexity for existence checks when looking for keys. - **Basic Looping**: A simple `for` loop can run through the array, but this will have a performance cost similar to `array.includes`. - **IndexOf or Find**: Older methods like `array.indexOf` or `array.find` offer similar functionality but are less semantically clear for checking inclusion. In conclusion, the benchmark demonstrates the clear performance advantages of using `set.has` over `array.includes` for membership checking in JavaScript, particularly in large collections or scenarios where performance is critical.
Related benchmarks:
Array includes 20190424
Array for vs. some
pre fill array
array.length === 0 vs !array.length
Array vs Set 2384
Set replace
Array creation for, map, foreach
Map.has vs Set.has vs Array.includes 01.04.2025
Array.at(i) vs Array[i]
Comments
Confirm delete:
Do you really want to delete benchmark?