Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (1kkk)
(version: 0)
Comparing performance of:
array.includes vs Set.has vs array.indexOf
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [...Array(1000000).keys()]; var b = new Set(a)
Tests:
array.includes
return a.includes(900000)
Set.has
return b.has(900000)
array.indexOf
return a.indexOf(900000)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
array.includes
Set.has
array.indexOf
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 options, pros and cons, and other considerations. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark test case created on MeasureThat.net. The test compares three different approaches for checking if an element exists in an array: 1. `array.includes()` 2. `Set.has()` 3. `array.indexOf()` (although this one is not directly comparing to the other two, it's included as a control) **Script Preparation Code** The script preparation code sets up two arrays: `a` and `b`. `a` is an array of 1 million keys generated using `[...Array(1000000).keys()]`, while `b` is a Set created from the keys of `a`. ```javascript var a = [...Array(1000000).keys()]; var b = new Set(a); ``` **Html Preparation Code** There is no HTML preparation code provided, which means that this benchmark only checks the performance of JavaScript code execution. **Test Cases** The test cases compare the performance of each approach: 1. `array.includes()`: Checks if an element exists in array `a` by calling `includes(900000)`. 2. `Set.has()`: Checks if an element exists in Set `b` by calling `has(900000)`. 3. `array.indexOf()`: Although not directly comparing to the other two, this test case is included for completeness. **Results** The latest benchmark result shows the performance data for each browser, device platform, and operating system: | Browser | Device Platform | Operating System | ExecutionsPerSecond | | --- | --- | --- | --- | | Chrome 88 | Desktop | Linux | 10794344.0 (Set.has) | | Chrome 88 | Desktop | Linux | 876.3388671875 (array.includes) | | Chrome 88 | Desktop | Linux | 873.6167602539062 (array.indexOf) | **Comparison of Approaches** Here's a comparison of the three approaches: 1. **`Set.has()`**: This approach is likely to be the fastest due to its use of hash tables, which provide constant-time performance for membership tests. * Pros: Fast and efficient. * Cons: Requires creating a Set object, which may incur additional memory overhead. 2. **`array.includes()`**: This approach uses the `includes()` method, which checks if an element exists in the array by iterating through it. * Pros: Easy to implement and doesn't require creating an extra data structure. * Cons: May be slower than `Set.has()` due to the iterative search. 3. **`array.indexOf()`**: Although not directly comparing to the other two, this test case is included for completeness. `array.indexOf()` returns the index of the first occurrence of a specified element or -1 if it's not found. * Pros: Can be used as an alternative to `includes()`. * Cons: Returns the index, which may not be desirable in all cases. **Other Considerations** When choosing an approach for this use case: * **Memory overhead**: If memory efficiency is a concern, `Set.has()` might not be the best choice due to the additional memory required for creating the Set object. * **Browser support**: The performance data suggests that Chrome 88 is the fastest browser for all three approaches. However, it's essential to test on other browsers and devices to ensure compatibility. In summary, `Set.has()` is likely to be the fastest approach due to its use of hash tables, but it may incur additional memory overhead. The choice between `array.includes()` and `array.indexOf()` depends on the specific requirements and preferences of the developer.
Related benchmarks:
convert to set + set.has vs. array.includes
set.has vs. array.includes (large)
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?