Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs array indexOf vs array inclues
(version: 0)
Comparing performance of:
Array includes vs Set has vs Array indexOf
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', 'height', 'width', 'maxHeight', 'maxWidth', 'maxHeight', 'minWidth', 'color', 'bg', 'backgroundColor', 'opacity', 'm', 'mt', 'mb', 'mr', 'mr', 'mx', 'my', 'p', 'pt', 'pb', 'pr', 'pl', 'px', 'py', 'border', 'boxShadow', 'flex', 'verticalAlign', 'textAlign', 'overflow', 'display', 'cursor' ]; var b = new Set(a)
Tests:
Array includes
return a.includes('cursor')
Set has
return b.has('mx')
Array indexOf
return a.indexOf('mx') >= 0
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):
Let's dive into the explanation of the provided JSON benchmark. **Benchmark Definition** The benchmark is designed to compare three different approaches for finding an element in arrays: `includes()`, `indexOf()` with equality comparison, and using a `Set` data structure. The purpose is to determine which approach is faster on average. **Options Compared** 1. **Array `includes()`**: This method checks if a specified value (in this case, `'mx'`) exists in the array. 2. **Array `indexOf()` with equality comparison**: This method returns the index of the first occurrence of the specified value (`'mx'`) in the array. If the value is not found, it returns -1. In the benchmark code, we're comparing this to an equality check using `>= 0` to simulate a more complex condition. 3. **Set data structure**: We create a new `Set` object containing the elements of the original array and then use the `has()` method to check if `'mx'` is present in it. **Pros and Cons** 1. **Array `includes()`**: * Pros: Simple, widely supported, and often used for quick checks. * Cons: May be slower for large arrays due to linear search. 2. **Array `indexOf()` with equality comparison**: * Pros: Can return the exact index of the element if found, making it useful for certain use cases. * Cons: Slower than `includes()` because it performs a linear search, and may return -1 even if the value is present in the array due to the inequality condition. 3. **Set data structure**: * Pros: O(1) lookup time on average, making it suitable for large datasets. * Cons: May require more memory and be less intuitive than using arrays or `includes()`. **Library Usage** In this benchmark, we're using the built-in JavaScript `Set` object, which is a data structure that automatically eliminates duplicate values. The `has()` method checks if a value is present in the set without having to iterate through all elements. **Special JS Feature/Syntax** None of the options used special JavaScript features or syntax in this benchmark. However, it's worth noting that the use of `Set` and its `has()` method relies on the internal implementation details of the JavaScript engine, which may change over time. **Other Alternatives** If you need to perform fast membership testing on large arrays, other alternatives include: * Using a `SparseArray` or similar data structure, which can store only non-zero elements. * Implementing your own binary search or hash-based lookup algorithm for arrays. * Using external libraries like `lodash` or `fast-levelling`, which provide optimized versions of these operations. Keep in mind that the choice of approach depends on the specific requirements and constraints of your use case.
Related benchmarks:
set.has vs. array.includes (string values)
set.has vs. array.includes vs array.indexOf (string values)
set.has vs. array.includes vs array.indexOf (string values) - longer
indexof vs set123
Comments
Confirm delete:
Do you really want to delete benchmark?