Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs Set.has with large data set
(version: 0)
Comparing performance of:
Array includes vs Set has
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var nums = Array(100000).fill().map((i,n)=>n)
Tests:
Array includes
let smallest=1 while(nums.includes(smallest)){ smallest++ } return smallest
Set has
nums = new Set(nums) let smallest=1 while(nums.has(smallest)){ smallest++ } return smallest
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array includes
Set has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array includes
2.1 Ops/sec
Set has
118.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark compares the performance of two approaches for checking if an element is present in a large dataset: `Array.includes()` and `Set.has()`. The dataset consists of 100,000 unique numbers generated using `Array(100000).fill().map((i,n)=>n)`. **Options Compared** Two options are compared: 1. **Array.includes()**: This method checks if an element is present in the array by iterating through it and checking for a match. 2. **Set.has()**: This method uses a Set data structure to store unique elements, allowing for fast lookups. **Pros and Cons** **Array.includes():** Pros: * Simple implementation * Works well with small to medium-sized datasets Cons: * Performance degrades significantly as the dataset size increases due to linear search. * May be slower than `Set.has()` for large datasets. **Set.has():** Pros: * Fast lookups using a Set data structure, making it suitable for large datasets. * Efficient use of memory, storing only unique elements. Cons: * Requires additional memory to store the Set data structure. * Implementation may be less intuitive for developers without prior experience with Sets. **Library Used** The `Set.has()` method uses the built-in `Set` data structure in JavaScript. A Set is a collection of unique values, and the `has()` method checks if an element is present in the set. **Special JS Feature or Syntax** No special features or syntax are used in this benchmark. However, it's worth noting that the use of Sets requires a basic understanding of data structures and algorithms in JavaScript. **Other Alternatives** 1. **Using Array.prototype.indexOf()**: Instead of `Array.includes()`, you can use `Array.prototype.indexOf()` to find the index of an element in the array. 2. **Using LINQ-like libraries**: For larger datasets, libraries like Lodash or Ramda provide more efficient alternatives for working with arrays and Sets. 3. **Native Web Workers**: If performance is critical, using web workers could help offload computationally intensive tasks to separate threads. **Benchmark Result Interpretation** The latest benchmark result shows that `Set.has()` performed significantly better than `Array.includes()`, executing approximately 57 times faster on the same dataset. This highlights the advantages of using a Set data structure for fast lookups, especially with large datasets.
Related benchmarks:
set.has vs. array.includes(million)
set.has vs. array.includes (10000 items)
set.has vs. array.includes on 25 000 items
set.has vs. array.includes (find 999,999 in 1,000,000)
Array.includes vs Set.has vas Map.has with large data set
Comments
Confirm delete:
Do you really want to delete benchmark?