Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes vs array.indexOf (string values)
(version: 0)
Comparing performance of:
includes vs lookup vs indexof
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [ '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:
includes
return a.includes('mx')
lookup
return b.has('mx')
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
includes
lookup
indexof
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 days ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:150.0) Gecko/20100101 Firefox/150.0
Browser/OS:
Firefox 150 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
37201584.0 Ops/sec
lookup
2784784640.0 Ops/sec
indexof
38033952.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Benchmark Overview** The benchmark compares the performance of three different methods for checking if a value exists within an array or set: 1. `array.includes(value)`: checks if a value is present in the array using the `includes()` method. 2. `set.has(value)`: checks if a value is present in the Set data structure using the `has()` method. 3. `array.indexOf(value)` (or, equivalently, `value >= 0 && array.includes(value)`) : checks if an index exists for a value in the array by attempting to find its position. **Options Compared** The three options are compared: * **`array.includes(value)`**: uses the `includes()` method of arrays. + Pros: simple and intuitive, widely supported across browsers. + Cons: can be slower than other methods for large datasets due to the need to traverse the array from start to end. * **`set.has(value)`**: uses the `has()` method of Sets. + Pros: fast and efficient, especially for large datasets since it only needs to check if the value is present in the set. + Cons: requires a Set data structure, which may not be available or supported by all browsers. * **`array.indexOf(value)`**: uses the `indexOf()` method of arrays (or an equivalent implementation using `includes() && value >= 0`). + Pros: simple and efficient for large datasets, as it only needs to find the index of the value if present. + Cons: slower than `set.has()` since it requires traversing the array from start to end. **Library Used** The benchmark uses JavaScript's built-in Set data structure. The Set data structure is a collection of unique values that can be used for efficient membership testing (i.e., checking if a value exists within the set). **Special JS Features or Syntax** This benchmark does not use any special JavaScript features or syntax beyond what's required to implement the three options being compared. **Other Alternatives** Other alternatives for these methods include: * **Using `includes()` with an array filter`: `array.filter(value => value === target).length > 0` + Pros: can be faster than `indexOf()` since it doesn't require traversing the entire array. + Cons: slower than `set.has()` due to the need to create a new filtered array. * **Using `in` operator**: `target in array || set.has(target)` + Pros: simple and efficient for checking membership, but may not be supported by older browsers or versions of JavaScript. It's worth noting that the choice of method will depend on the specific requirements of your use case, such as the size of the dataset, the available resources (e.g., CPU power, memory), and the trade-offs between simplicity, performance, and code readability.
Related benchmarks:
set.has vs. array.includes (string values)
set.has vs. array.includes (random string values)
set.has vs. array.includes vs array.indexOf (string values) - longer
set.add vs array.push maan
Comments
Confirm delete:
Do you really want to delete benchmark?