Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
new Set([x]).has vs [x].includes
(version: 1)
Comparing performance of:
includes vs has
Created:
5 years ago
by:
Registered User
Jump to the latest result
Tests:
includes
return [42].includes(42)
has
return new Set([42]).has(42)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
29736040.0 Ops/sec
has
1861505.9 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares two ways to check if an element exists in a list-like structure: **1. `includes` Method:** This method is built into JavaScript arrays. It takes a value as an argument and returns `true` if the value exists in the array, otherwise `false`. * **Pros:** Concise and readable syntax. * **Cons:** May be slightly slower than using `has` for very large sets due to its inherent iteration through elements. **2. `Set.has` Method:** This method is part of the JavaScript Set object, which only stores unique values. It takes a value as an argument and returns `true` if the value exists in the set, otherwise `false`. In this benchmark, a new Set is created with the given array's elements before checking for the existence of the target value. * **Pros:** Potentially faster than `includes` for large sets because Sets are optimized for membership checks (using hash tables internally). * **Cons:** Requires creating a new Set object, which incurs some overhead compared to simply using a built-in array method. **Alternatives** While `includes` and `has` are the two methods being benchmarked, there are other ways to check for an element's existence: * **Manual Iteration (Loop):** You could loop through the elements of an array and compare each element to the target value. This is less efficient than using built-in methods. * **Finding Index:** The `indexOf` method returns the index of a value in an array, or -1 if it's not found. The choice between these methods depends on factors like: * **Array Size:** For small arrays, the performance difference may be negligible. For large arrays, `has` using Sets might be significantly faster. * **Uniqueness Requirement:** If you need to ensure uniqueness of elements, using a Set is beneficial regardless of size. **In summary:** The benchmark tests whether using the built-in `includes` method or creating a Set and using its `has` method for membership checks provides better performance.
Related benchmarks:
convert to set + set.has vs. array.includes
array.includes vs. set.has on the fly
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?