Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes big data 2
(version: 1)
Comparing performance of:
includes vs lookup
Created:
10 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [...Array(100000).keys()]; var b = new Set(a)
Tests:
includes
return a.includes(5000)
lookup
return b.has(5000)
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:
10 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
134326.1 Ops/sec
lookup
93949792.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 10 months ago):
The benchmark described involves comparing two different methods for checking whether a specific element exists within a collection: using an array with the `includes` method and a Set with the `has` method. The particular case tested here is finding if the number `5000` is included in a large dataset. ### Benchmark Setup In the preparation stage of the benchmark, an array `a` containing integers from `0` to `99999` is created using the spread operator and `Array.keys()`. A Set `b` is then created from this array. The purpose of utilizing a Set instead of an array is that Sets are optimized for certain operations, including membership testing. ### Test Cases 1. **`a.includes(5000)`**: - **Method**: This uses the `includes` method provided by the Array prototype, which checks if the array contains the specified element (`5000` in this case). - **Performance Characteristics**: - **Pros**: - Simple and straightforward; easy to understand and use. - Native method; works directly on arrays and can be helpful when dealing with arrays of various types. - **Cons**: - Time complexity is O(n) in the worst case because it may need to iterate through the entire array to find the element. 2. **`b.has(5000)`**: - **Method**: This uses the `has` method of the Set object, which checks for the presence of the specified element. - **Performance Characteristics**: - **Pros**: - Significantly faster membership tests; average time complexity is O(1) because Sets utilize hash tables under the hood. - Better suited for scenarios primarily focused on checking containment efficiently. - **Cons**: - Requires creation of a Set, which incurs additional memory overhead. This overhead can be significant depending on the size of the dataset. - Less familiar to developers targeting traditional arrays exclusively, although this is changing as Sets become more commonly used in modern JavaScript. ### Results Overview The benchmark results show that: - The `lookup` test (using the Set's `has` method) achieved **93,949,792 executions per second**. - The `includes` test achieved **134,326.14 executions per second**. This stark difference highlights the efficiency of the Set data structure for membership tests, validating its utility in scenarios dealing with large datasets where performance is critical. ### Alternatives While the presented methods (`includes` and `has`) are common for checking membership, other alternatives also exist: - **Using a plain object**: For checking membership, one could convert the array to an object with keys corresponding to the array elements. However, this comes with its own trade-offs, particularly regarding memory usage and complexity of implementation. - **Using a library**: One could utilize libraries like lodash or underscore.js, which provide additional utility functions. However, for simple membership checks, using plain JavaScript is often sufficient. ### Conclusion The choice between using an array's `includes` method and a Set's `has` method hinges on the performance needs of the application. For applications requiring frequent existence checks within large datasets, using a Set is highly advantageous. In contrast, if simplicity and immediate readability are priorities and the dataset is small, then `includes` may be preferable. Understanding the underlying data structures and their performance implications is crucial for making the appropriate choice in a given context.
Related benchmarks:
set.has vs. array.includes (1m)
set.has vs. array.includes(million)
set.has vs. array.includes on big arrays
set.has vs. array.includes on big arrays s
set.has vs. array.includes 1million
set.has vs. array.includes (large)
set.has vs. array.includes (10000 items)
set.has vs. array.includes (large: 100)
set.has vs. array.includes (large: 10000)
set.has vs. array.includes big data
Comments
Confirm delete:
Do you really want to delete benchmark?