Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Chilinh --> RegEx.test vs. String.includes vs. String.match vs String.IndexOf
(version: 0)
Comparing performance of:
RegEx.test vs String.includes vs String.match vs String.indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "lexi.fletcher23@gmail.com|Gymeast1!"; var regex = /@yahoo\.|@gmail\.|@hotmail\.|@live\.|@outlook\./;
Tests:
RegEx.test
regex.test(string);
String.includes
string.includes("@yahoo.") || string.includes("@gmail.") || string.includes("@hotmail.") ||string.includes("@live.")||string.includes("@outlook.");
String.match
string.match(regex);
String.indexOf
string.indexOf("@yahoo.") || string.indexOf("@gmail.") || string.indexOf("@hotmail.") ||string.indexOf("@live.")||string.indexOf("@outlook.");
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.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):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of three different string matching approaches: 1. `RegEx.test()` 2. `String.includes()` (using multiple OR conditions) 3. `String.match()` (using a regular expression) Each test case is designed to measure the execution speed of each approach on a specific input string. **RegEx.test()** `RegEx.test()` is a method that tests whether the entire specified pattern exists within the text. It returns `true` if the pattern is found, and `false` otherwise. Pros: * Simple to use and understand * Efficient for most cases Cons: * Can be slow for large patterns or complex regular expressions * May not work as expected with certain types of input (e.g., non-ASCII characters) **String.includes()** `String.includes()` checks if a specified string exists within the input string. It returns `true` if the substring is found, and `false` otherwise. The benchmark uses multiple OR conditions to check for different email domains (@yahoo.com, @gmail.com, etc.). This approach can lead to slower performance compared to using RegEx directly. Pros: * Simple to use and understand * Can be faster than using RegEx for small inputs Cons: * Less efficient for large patterns or complex regular expressions * May not work as expected with certain types of input (e.g., non-ASCII characters) **String.match()** `String.match()` searches the entire string for a specified pattern. It returns an array of matches if found, and `null` otherwise. The benchmark uses a RegEx to match email domains (@yahoo.com, @gmail.com, etc.). This approach can be more efficient than using RegEx.test() or String.includes(), but may still be slower than using the optimized RegEx directly. Pros: * Can be faster than using RegEx.test() for small inputs * More flexible than String.includes() Cons: * Less straightforward to use and understand compared to RegEx.test() * May not work as expected with certain types of input (e.g., non-ASCII characters) **Other Considerations** * The benchmark uses Chrome 116, which may affect the results due to its optimized engine. * The input string is hardcoded, which might not reflect real-world usage scenarios. * The benchmark only measures performance for email domain matching, which limits the scope of the test. **Alternatives** If you want to explore other string matching approaches or optimize your code, consider using: 1. `String.prototype.includes()` (ES6+): This method is similar to String.includes(), but is optimized for modern browsers. 2. `RegExp.test()`: This method tests whether a regular expression matches a specified pattern within a text. 3. `String.prototype.replace()`: This method can be used with a regular expression to extract specific parts of the input string. Keep in mind that the performance and efficiency of these alternatives may vary depending on your use case and browser environment.
Related benchmarks:
RegEx.test vs. String.includes vs. String.match insensitive
RegEx.test vs. String.includes vs. String.match in case insensitive scenarios
Case insensitive RegEx.test vs. String.includes when string doesn’t match
RegEx.test vs. String.includes incasesensitive
regex vs includes - case insensitive
Comments
Confirm delete:
Do you really want to delete benchmark?