Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.match vs String.search
(version: 0)
<b>Comparing performance of:</b> RegEx.test vs String.includes vs String.match vs String.search
Comparing performance of:
RegEx.test vs String.includes vs String.match vs String.search
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.match
string.match("Hello");
String.search
string.search("Hello");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
RegEx.test
String.includes
String.match
String.search
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 benchmark. **Benchmark Overview** The benchmark compares the performance of four different methods for testing if a string contains a specific substring: `RegExp.test()`, `String.includes()`, `String.match()`, and `String.search()`. **Methods Compared** 1. **`RegExp.test()`**: This method uses a regular expression engine to test if the entire input string matches the specified pattern. 2. **`String.includes()`**: This method checks if the input string contains the specified substring, without performing any actual matching. 3. **`String.match()`**: This method returns an array containing all non-overlapping matches of the specified pattern in the input string, or `null` if no match is found. It's similar to `RegExp.test()`, but returns an array instead of a boolean value. 4. **`String.search()`**: This method returns the index of the first occurrence of the specified substring in the input string, or `-1` if no match is found. **Pros and Cons** * **`RegExp.test()`**: + Pros: Can be used to test for exact matches, including edge cases like empty strings. + Cons: May be slower than other methods due to the overhead of compiling a regular expression. * **`String.includes()`**: + Pros: Fastest method among those compared, as it only checks if the substring is present in the input string without performing actual matching. + Cons: Returns `true` for substrings that don't actually appear in the input string, which may be misleading. * **`String.match()`**: + Pros: Can be used to get all matches of a pattern in a single operation. + Cons: Slower than `RegExp.test()`, as it returns an array instead of a boolean value. * **`String.search()`**: + Pros: Fastest method among those compared, as it only returns the index of the first occurrence without performing actual matching. + Cons: Returns `-1` for no match, which may be misleading. **Libraries and Special Features** None of the methods rely on external libraries. However, `RegExp.test()` uses the built-in regular expression engine to test for patterns in strings. No special JavaScript features or syntax are used in this benchmark. **Alternatives** Other methods for testing if a string contains a substring include: * Using `indexOf()` method: This method returns the index of the first occurrence of the specified substring, or `-1` if no match is found. * Using `toLowerCase()` and `includes()`: This method checks if the input string contains the specified substring in a case-insensitive manner. Note that the choice of method depends on the specific requirements of your use case. If you need to test for exact matches, `RegExp.test()` or `String.match()` might be suitable. If you only care about whether the substring is present in the input string without performing actual matching, `String.includes()` might be faster.
Related benchmarks:
RegEx.test vs. String.includes vs. String.match insensitive
Comparing performance of: String.search vs String.match
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?