Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes 2233344
(version: 1)
Comparing performance of:
has vs includes
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array(1000000).fill(undefined).map((_, i) => i); var b = new Set(a)
Tests:
has
for (let i = 500000; i < 600000; i++) b.has(i)
includes
for (let i = 500000; i < 600000; i++) a.includes(i)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
has
includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
has
12423.8 Ops/sec
includes
0.3 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the JSON is designed to compare the performance of two different methods for checking the existence of values within a collection in JavaScript: `Set.has()` and `Array.includes()`. Here's a breakdown of what is tested and the relevant considerations: ### Options Compared 1. **Set.has()** - **Definition**: Utilizes the `Set` data structure introduced in ES6, which allows you to store unique values of any type, whether primitive values or object references. - **Benchmark Code**: `for (let i = 500000; i < 600000; i++) b.has(i)` 2. **Array.includes()** - **Definition**: Checks if a given value exists in an array. It returns a boolean (`true` or `false`) based on the presence of the element. - **Benchmark Code**: `for (let i = 500000; i < 600000; i++) a.includes(i)` ### Performance Results - **Set.has()** achieved an execution rate of approximately **12,423.81 executions per second**. - **Array.includes()** achieved an execution rate of approximately **0.25 executions per second**. ### Pros/Cons of Each Approach #### Set.has() **Pros:** - **Optimal Performance**: `Set` is designed for fast lookups, operating in constant time (O(1)) on average. This makes it significantly faster for membership checks as demonstrated in the benchmark results. - **Uniqueness**: Automatically handles storage of unique elements, preventing duplicates which can help save memory and avoid unintended behavior. **Cons:** - **Memory Overhead**: The `Set` can have a higher memory footprint compared to traditional arrays, especially if the collection is large. - **Initialization Overhead**: Creating a `Set` from an array may take additional time/memory than simply using an array. #### Array.includes() **Pros:** - **Simplicity**: Offers a simple and straightforward syntax for developers. Easier to understand for those already accustomed to working with arrays. - **No Overhead**: No need to convert data structures; works directly with existing arrays. **Cons:** - **Slower Performance**: The search complexity is linear (O(n)), meaning it must iterate through the array to find a match, making it less efficient for large datasets, as indicated by the benchmark results. - **Potential Duplicates**: Does not inherently enforce uniqueness, which can complicate checks if duplicates are present. ### Considerations and Alternatives 1. **Alternative Data Structures**: Depending on the specific needs, other structures such as Maps (for key-value pairs) or even plain objects might be used for specific tasks rather than Array or Set. 2. **Use Cases**: - Use `Set` when frequent membership checks are necessary, especially with larger datasets. - Use `Array.includes()` when an array's order and the ability to store duplicates is essential, and performance is not as critical. 3. **Performance Tuning**: If performance with arrays is necessary, consider sorting prior to searches or utilizing binary search techniques if the array is sorted. 4. **JavaScript Engines**: Different JavaScript engines may yield slightly different performance results. While this benchmark is with Chrome, testing on different environments (Node.js, Firefox, etc.) might yield varied execution rates. 5. **Contextual Usage**: The selection between `Set` and `Array` should be context-driven based on data structure requirements, performance needs, and functionality. This benchmarking comparison highlights the significant performance advantages of using `Set` for membership checks over `Array.includes()`, particularly in scenarios dealing with large datasets.
Related benchmarks:
Set Has vs Array Includes
set.has vs. array.includes large
set.has vs. array.includes large 1
set.has vs. array.includes large 2
set.has vs. array.includes large 3
set.has vs. array.includes (large array)
set.has vs. array.includes (1 million entries)
set.has vs. array.includes - large array
set.has vs. array.includes 223334
Comments
Confirm delete:
Do you really want to delete benchmark?