Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (1 million entries)
(version: 0)
Comparing performance of:
array.includes vs set.has
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = []; for(let i = 0; i < 1000000; i++) { a.push(i); } var b = new Set(a)
Tests:
array.includes
return a.includes(9)
set.has
return b.has(9)
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The benchmark measures the performance difference between using `Array.includes()` and `Set.has()` to search for an element in two arrays with 1 million entries. The test case uses a JavaScript library (a simple array and Set implementation). **Script Preparation Code** The script preparation code creates two arrays: 1. `a`: An array of 1 million integers, populated using a `for` loop. 2. `b`: A new `Set` object containing the elements of `a`. **Html Preparation Code** There is no HTML preparation code provided. **Individual Test Cases** There are two test cases: 1. **Array.includes()** * Benchmark Definition: `return a.includes(9)` * Purpose: Measure the performance of using `Array.includes()` to search for an element in array `a`. 2. **Set.has()** * Benchmark Definition: `return b.has(9)` * Purpose: Measure the performance of using `Set.has()` to search for an element in set `b`. **Pros and Cons** Here are some pros and cons of each approach: 1. **Array.includes()** * Pros: + Widely supported by most browsers. + Easy to use and understand. + Can handle arrays with large numbers of elements efficiently. * Cons: + May have performance issues for very large arrays due to the need to scan the entire array. 2. **Set.has()** * Pros: + Fast lookup times, especially for large datasets (O(1) on average). + Can be more efficient than `Array.includes()` for very large arrays. * Cons: + May not be as widely supported by older browsers. + Requires a Set object to be created beforehand. **Library: Simple Array and Set Implementation** The test case uses a simple array and set implementation, which is likely just for demonstration purposes. In real-world scenarios, you would typically use built-in JavaScript arrays and sets. **Other Considerations** * **Data Distribution**: The benchmark assumes that the data in both arrays is evenly distributed (i.e., every number appears an equal number of times). This may not always be the case in real-world datasets. * **Browser Variability**: Different browsers may have varying levels of support for `Array.includes()` and `Set.has()`, which can affect performance. **Alternatives** If you wanted to compare the performance of other methods, such as: 1. **Regular expressions (regex)**: You could use a regex to search for the element in the array. 2. **Binary search**: If the data is sorted, binary search can be a more efficient approach. 3. **Other set-based operations**: Depending on your specific requirements, you might want to test other set-based operations like `Set.size()` or `Set.delete()`.
Related benchmarks:
set.has vs. array.includes large 1
set.has vs. array.includes - large array
set vs array iteration 100k elements
set.has vs. array.includes (find 999,999 in 1,000,000)
Comments
Confirm delete:
Do you really want to delete benchmark?