Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String in Array - includes() vs indexOf() vs test()
(version: 0)
Comparing performance of:
includes() vs indexOf() vs test()
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
includes()
['apple', 'orange', 'lemon', 'blueberry'].includes('lemon')
indexOf()
['apple', 'orange', 'lemon', 'blueberry'].indexOf('lemon') > -1
test()
/^(apple|orange|lemon|blueberry)$/.test('lemon')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes()
indexOf()
test()
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 provided JSON and benchmark preparation code to understand what's being tested. **Benchmark Definition** The benchmark is defined as three test cases that compare the performance of different string methods: `includes()`, `indexOf()`, and `test()`. **Options Compared** 1. **`includes()`**: The `includes()` method checks if a specified value (in this case, 'lemon') is present in an array or string. 2. **`indexOf()`**: The `indexOf()` method finds the index of the first occurrence of a specified value (in this case, 'lemon') in an array or string. If not found, it returns -1. 3. **`test()`**: The `test()` method is a part of the regular expression API and checks if a string matches a given pattern. **Pros and Cons** * **`includes()`**: * Pros: Simple to use, efficient for array lookups. * Cons: Can be slower than other methods for large arrays or strings due to the linear search algorithm. * **`indexOf()`**: * Pros: Returns the index of the found value, which can be useful in some cases. Generally faster than `includes()`. * Cons: Can be slower than other methods if not finding the value (incurring a linear search). * **`test()`**: * Pros: More flexible for pattern matching and can handle complex regex patterns. * Cons: May be slower due to the overhead of compiling and executing regular expressions. Also, it's more specific to regex use cases. **Library Used** In this benchmark, the `includes()` method is a built-in JavaScript method that doesn't rely on any external library. The `indexOf()` method also comes with JavaScript. However, `test()` uses a library (the `RegExp` object) for regular expressions. **Special JS Feature/Syntax** There's no special feature or syntax in this benchmark besides the use of regular expressions and built-in string methods. **Other Alternatives** In addition to the provided methods, you could compare other approaches such as: * **Using `slice()` or `subarray()`**: Extracting a subset of an array or creating a new array with a specific index range. * **Using numerical indexing directly on arrays or strings**: Accessing elements by their index rather than using string-based lookups. Here's an example for the first test case: ```javascript const arr = ['apple', 'orange', 'lemon', 'blueberry']; console.log(arr[2]); // Output: lemon ``` Note that this approach is faster but does not provide the same flexibility as `includes()` or `indexOf()`.
Related benchmarks:
IndexOf vs Includes
Array.includes() vs Array.indexOf()
Array find with indexOf vs includes
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?