Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.match(Regex)
(version: 0)
Comparing performance of:
RegEx.test vs String.includes vs String.match
Created:
2 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(regex);
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.match
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 explain what's being tested. **Benchmark Definition** The benchmark is comparing three different approaches to check if a string contains or matches a specific pattern: 1. **`regex.test(string)`**: This method uses the `test()` function of a RegExp object, which tests whether the entire string matches the regular expression. 2. **`string.includes("Hello")`**: This method checks if the string contains the specified substring ("Hello"). 3. **`string.match(regex)`**: This method returns an array containing the full match (if found) and any capturing groups (if defined). **Comparison** The three approaches are compared in terms of performance, specifically the number of executions per second. **Pros and Cons** * **`regex.test(string)`**: + Pros: Can be more accurate than `includes()` or `match()`, especially for complex patterns. + Cons: May have slower performance due to the overhead of creating and executing a RegExp object. * **`string.includes("Hello")`**: + Pros: Fast and simple, but may not be as efficient as other methods for large strings. + Cons: Can lead to false positives if "Hello" is part of another substring. * **`string.match(regex)`**: + Pros: Can return more information than just a boolean value (e.g., capturing groups). + Cons: May have slower performance due to the overhead of creating and executing a RegExp object. **Library and Special JS Feature** There is no explicit library mentioned in the benchmark definition, but `String.includes()` is a built-in JavaScript method. However, it's worth noting that some browsers may optimize this method using a specialized algorithm or data structure (e.g., `includes()` uses a "trie" data structure on Chrome). **Other Considerations** The test cases use a simple string ("Hello world!") and a regular expression ("/Hello/") to make the comparison more straightforward. In real-world scenarios, you might need to consider other factors, such as: * The length of the input strings * The complexity of the patterns * The presence of special characters or non-ASCII characters * The specific requirements of your use case (e.g., accuracy vs. performance) **Alternatives** Other alternatives for string matching and pattern searching in JavaScript include: * `String.prototype.indexOf()` or `String.prototype.lastIndexOf()`: These methods return the index of the first or last occurrence of a substring, respectively. * `Regexp.exec()`: This method returns an array containing the full match (if found) and any capturing groups (if defined). * `RegExp.test()`: Similar to `regex.test(string)`, but can be used as a standalone function. Keep in mind that each alternative has its own trade-offs in terms of performance, accuracy, and features.
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
Long regex test vs string includes
Longer regex test vs string includes
Comments
Confirm delete:
Do you really want to delete benchmark?