Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes at scale
(version: 0)
Comparing performance of:
includes (hit) vs lookup (hit) vs includes (miss) vs lookup (miss)
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array.from(new Array(100000).keys()); var b = new Set(a)
Tests:
includes (hit)
return a.includes(99999)
lookup (hit)
return b.has(99999)
includes (miss)
return a.includes(100001)
lookup (miss)
return b.has(100001)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
includes (hit)
lookup (hit)
includes (miss)
lookup (miss)
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):
Let's break down the benchmark and its various components. **Benchmark Definition JSON** The provided JSON defines a JavaScript benchmark that compares two approaches for checking if an element exists in an array: `set.has` (lookup) and `array.includes`. The benchmark aims to measure the performance of these two methods at scale, by creating a large set and array from 0 to 99,999. **Script Preparation Code** The script preparation code creates: 1. A large array `a` containing 100,000 elements from 0 to 99,999 using `Array.from(new Array(100000).keys())`. 2. A new Set object `b` initialized with the elements of array `a`. This setup prepares the test environment by creating a large dataset for comparison. **Individual Test Cases** The benchmark consists of four individual test cases: 1. **includes (hit)**: This test case checks if an element exists in the array `a` using the `includes()` method and passes a value that is present in the array (99,999). 2. **lookup (hit)**: This test case checks if an element exists in the Set object `b` using the `has()` method and passes a value that is present in the set. 3. **includes (miss)**: This test case checks if an element exists in the array `a` using the `includes()` method and passes a value that is not present in the array (100,001). 4. **lookup (miss)**: This test case checks if an element exists in the Set object `b` using the `has()` method and passes a value that is not present in the set. **Benchmark Results** The latest benchmark results are provided for each of the four test cases: 1. **includes (hit)**: Chrome 105, Desktop, Mac OS X 10.15.7, with an execution rate of approximately 163,746 executions per second. 2. **lookup (hit)**: Chrome 105, Desktop, Mac OS X 10.15.7, with an execution rate of approximately 162,573 executions per second. 3. **includes (miss)**: Chrome 105, Desktop, Mac OS X 10.15.7, with an execution rate of approximately 119,964 executions per second. 4. **lookup (miss)**: Chrome 105, Desktop, Mac OS X 10.15.7, with an execution rate of approximately 118,967 executions per second. **Comparison and Analysis** The benchmark results show that: * The `includes()` method is faster for both hit and miss cases compared to the `has()` method (lookup). * The `has()` method (lookup) performs better when the value exists in the set, but slower for missing values. * The execution rates are similar between hit and miss cases for both methods. **Pros and Cons of Approaches** 1. **array.includes**: Pros: * Faster for large arrays. * Easy to use and understand. Cons: * May be slower for smaller datasets due to overhead. 2. **set.has**: Pros: * More efficient for large sets, as they can take advantage of hash table optimizations. * Can be faster than `includes()` for missing values. Cons: * Requires a Set object, which may have additional memory and initialization costs. **Other Considerations** 1. **JavaScript Features**: This benchmark does not explicitly test any special JavaScript features or syntax. 2. **Alternative Implementations**: Other approaches to check if an element exists in an array or set might include using `indexOf()`, `binarySearch()`, or custom iteration methods. In summary, this benchmark provides a comparison between the performance of two common approaches for checking if an element exists in an array: `array.includes` and `set.has`. The results suggest that `array.includes` is generally faster for both hit and miss cases, but `set.has` may offer better performance for large sets.
Related benchmarks:
convert to set + set.has vs. array.includes
set.has vs. array.includes (large)
array.includes vs. set.has on the fly
set.has vs. array.includes (find 999,999 in 1,000,000)
Array includes vs Set.has
Comments
Confirm delete:
Do you really want to delete benchmark?