Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.indexOf
(version: 0)
Comparing performance of:
RegEx.test vs String.includes vs String.indexOf
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "Hello world!"; var regex = /Hello/;
Tests:
RegEx.test
regex.test(string);
String.includes
string.includes("Hello");
String.indexOf
string.indexOf("Hello")!=1;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
RegEx.test
String.includes
String.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):
**Benchmark Overview** The provided benchmark measures the performance of three different string searching methods in JavaScript: RegEx's `test()` method, `String.includes()`, and `String.indexOf()`. The goal is to determine which method is the most efficient. **Options Compared** 1. **RegEx's `test()` method**: This method returns a boolean value indicating whether the regular expression matches at the specified position in the string. 2. **`String.includes()`**: This method returns a boolean value indicating whether the string contains the specified value (regex pattern). 3. **`String.indexOf()`**: This method returns the index of the first occurrence of the specified value (regex pattern) in the string, or -1 if not found. **Pros and Cons** * **RegEx's `test()` method**: + Pros: Can be more efficient than other methods for simple cases, as it avoids unnecessary iterations. + Cons: May be slower for complex regex patterns due to the overhead of compiling and executing the pattern. Additionally, it returns a boolean value, which can lead to unnecessary computations if the result is not needed. * **`String.includes()`**: + Pros: Simple and efficient, as it only needs to iterate through the string until the first occurrence of the specified value. + Cons: May be slower for large strings or complex regex patterns due to the overhead of iterating through the entire string. Additionally, it returns a boolean value, which can lead to unnecessary computations if the result is not needed. * **`String.indexOf()`**: + Pros: Efficient and simple, as it only needs to iterate through the string until the first occurrence of the specified value. Returns an index value, which can be useful for further processing. + Cons: May return -1 in case of no match, which can lead to unnecessary computations. **Library Used** None explicitly mentioned in the benchmark definition. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax used in this benchmark. The tests only utilize built-in methods and operators. **Other Alternatives** In addition to the three options compared in this benchmark, other string searching methods in JavaScript include: * `String.prototype.match()` * `Array.prototype.includes()` * `String.prototype.search()` These methods may offer different trade-offs between performance, simplicity, and readability.
Related benchmarks:
RegEx.test vs. String.includes vs. String.match insensitive
RegEx.test vs. String.includes incasesensitive
RegEx.test (with inline regex) vs. String.includes vs. String.match
Longer regex test vs string includes
Comments
Confirm delete:
Do you really want to delete benchmark?