Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (length : 3)
(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]; var b = new Set(a)
Tests:
includes
return a.includes(3)
lookup
return b.has(3)
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
26954134.0 Ops/sec
lookup
38090536.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
This benchmark compares the performance of two methods for checking if a particular value exists within a collection: using `Array.includes()` for arrays and `Set.has()` for sets, specifically with a predefined dataset of integers. ### What is being tested? - **Array.includes(3)**: This test checks if the value `3` is present in an array `a` which contains numbers from 1 to 10. - **Set.has(3)**: This test checks if the value `3` is present in a `Set` `b` that was created from the same array. ### Options Compared 1. **Array.includes()** - **Pros**: - Simple syntax that is easy to read and understand for newcomers. - Suitable for small arrays or when performance is not a critical factor. - **Cons**: - The method performs a linear search through the array, resulting in O(n) time complexity in the worst case. This means that as the size of the array increases, the time it takes to check for an element also increases linearly, making it less efficient for larger collections. 2. **Set.has()** - **Pros**: - Hash table-based implementation allows for O(1) average time complexity for lookups. This means that it can check for the existence of an element in constant time, regardless of the size of the set. - Particularly efficient for large datasets where frequent lookups are necessary. - **Cons**: - Requires additional memory to create the `Set`, as it must store each unique item and its associated hash. - Depending on browser implementations and optimizations, hash table overhead may result in additional memory usage. ### Library and Features Used In this benchmark, no external library is used; it relies purely on native JavaScript features. Both `Array` and `Set` are built-in JavaScript objects, and their methods `includes()` and `has()` are part of standard ECMAScript functionality. ### Alternative Approaches 1. **Manual Iteration**: Instead of using `Array.includes()`, one could manually iterate over the elements of an array using a loop. While this could offer slight flexibility (e.g., for custom equality checks), it would still be O(n) and generally less readable. 2. **Other Data Structures**: Consider using other data structures like Maps or Typed Arrays based on specific use cases. Maps would allow for key-value pair storage, yet for simple existence checks, Sets remain the most efficient. 3. **Filtered Array**: Instead of utilizing `includes()`, one could filter the array to find if the value exists. However, this returns an array of matches and is more costly performance-wise (O(n)). ### Conclusion The benchmark demonstrates a performance contrast between using an array with linear search and a set with constant-time searching capability. In scenarios where you frequently need to check for the presence of elements within collections, using a `Set` is generally more efficient, especially as the size of the dataset grows. Understanding these differences can help software engineers choose the appropriate data structures based on performance needs.
Related benchmarks:
set.has vs. array.includes
set.has vs. array.includes on big arrays
set.has vs. array.includes perf
set.has vs. array.find
set.has vs. array.includesdd
set.has vs. array.includes 2333
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?