Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes 223334
(version: 1)
Comparing performance of:
includes vs lookup
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:
includes
for (let i = 500000; i < 600000; i++) a.includes(i)
lookup
for (let i = 500000; i < 600000; i++) b.has(i)
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 (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
includes
0.3 Ops/sec
lookup
12387.2 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark aims to compare the performance of two different methods for checking the presence of elements in a collection: using an array with the `includes` method versus using a `Set` with the `has` method. The benchmark consists of preparation code, test cases, and the results from executing those tests. ### Benchmark Overview 1. **Preparation Code**: - An array `a` is created with 1,000,000 elements, where the elements are the integers from 0 to 999,999. - A `Set` called `b` is constructed from this array. Sets in JavaScript offer unique values and efficient lookups. 2. **Test Cases**: - **`includes` Test Case**: This test loops through integers from 500,000 to 599,999 to check if each integer is included in the array `a` using the `includes` method. - **`lookup` Test Case**: This test similarly loops but checks for the presence of each integer in the set `b` using the `has` method. ### Performance Results The benchmark results show that the `has` method on a `Set` outperformed the `includes` method on an array: - **Lookup (Set)**: Executions per second: ~12,387 - **Includes (Array)**: Executions per second: ~0.25 ### Comparison of Approaches **Option 1: `Array.includes`** - **Pros**: - Simple and straightforward to implement. - Familiar to developers already using arrays in JavaScript. - **Cons**: - Performance degrades linearly with the size of the array. The time complexity is O(n) because it needs to iterate through the entire array to find the element. **Option 2: `Set.has`** - **Pros**: - Fast lookups with average time complexity of O(1), because sets are typically implemented using hash tables. - Efficient for scenarios requiring frequent lookups and where the uniqueness of items is important. - **Cons**: - Slightly more overhead in creating the Set from an array, especially for large datasets. - If the dataset frequently changes, maintaining a Set can require additional operations (insertions/deletions). ### Other Considerations - **Memory Usage**: Sets generally consume more memory than arrays because they store unique values and the structures needed for efficient lookup. - **Order of Elements**: Arrays maintain the order of their elements, while Sets do not. If the order of insertion is important, arrays may be preferable despite the slower lookup times. - **Alternatives**: - If the dataset cannot be too large or when performance is not critical, using simple arrays with `includes` is sufficient. - For large datasets or frequent lookups, an additional alternative could be using a JavaScript object (or a Map) for key/value pairs, but this behaves more like a hash table with its pros and cons regarding performance and element uniqueness. In summary, this benchmark effectively demonstrates how the inherent characteristics of JavaScript data structures impact performance as the size of the dataset increases. For tasks requiring frequent membership testing, using a Set is generally recommended for performance benefits.
Related benchmarks:
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 (10000 items)
set.has vs. array.includes 1000
set.has vs. array.includes (large array)
set.has vs. array.includes - large array
set.has vs. array.includes 2233344
Comments
Confirm delete:
Do you really want to delete benchmark?