Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.match v2
(version: 0)
Comparing performance of:
RegEx.test vs String.includes vs String.match
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "W0rLd Hello world!"; var regex = /Hello/;
Tests:
RegEx.test
regex.test(string);
String.includes
string.includes("Hello");
String.match
string.match("Hello");
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 benchmark and explain what's being tested, along with the pros and cons of each approach. **Benchmark Overview** The benchmark compares three different methods to search for a substring within a string: `RegEx.test()`, `String.includes()`, and `String.match()` (specifically version 2). The test case uses a predefined JavaScript script that creates a string variable `string` with the content "W0rLd Hello world!" and a regular expression variable `regex` with the pattern "/Hello/". **Method Overview** ### RegEx.test() The `RegEx.test()` method tests if the string matches the regular expression. It returns `true` if there's a match, or `false` otherwise. Pros: * Can be used for more complex matching scenarios * Allows specifying different flags (e.g., global, multiline) Cons: * Can be slower than simpler methods due to regex engine overhead ### String.includes() The `String.includes()` method checks if the string contains the specified value. It returns `true` if found, or `false` otherwise. Pros: * Fast and efficient * Simple syntax * Works well for simple substring matching Cons: * May not work as expected with special characters or edge cases * Not suitable for complex regex scenarios ### String.match() The `String.match()` method returns an array of matches if the string matches the regular expression. If no match is found, it returns `null`. Pros: * Can be used for more complex matching scenarios (like capturing groups) * Allows specifying different flags (e.g., global, multiline) Cons: * Returns an array or null, which can make results harder to interpret * May not work as expected with special characters or edge cases **Library Used** None of the methods listed above rely on a specific library. However, if you're using a modern JavaScript environment, it's likely that `String.includes()` is optimized and provided by the browser (e.g., Chrome). **Special JS Features/Syntax** The benchmark doesn't use any special JavaScript features or syntax, other than the regular expression engine. **Other Alternatives** If you needed to search for a substring within a string in JavaScript, some alternative approaches could include: * Using `String.indexOf()` instead of `String.includes()` * Creating a custom function using `String.prototype.replace()` with a callback function * Utilizing third-party libraries like regex engines or string manipulation utilities Keep in mind that the performance and efficiency of these alternatives may vary depending on the specific use case. **Benchmark Results Interpretation** The benchmark results show the execution per second (in executions/s) for each method, measured by Chrome 105 on a Windows desktop. The highest execution rate is achieved by `String.includes()`, followed closely by `RegEx.test()`.
Related benchmarks:
RegEx.test vs. String.includes vs. String.match insensitive
RegEx.test vs String.includes
RegEx.test vs. String.includes incasesensitive
RegEx.test (with inline regex) vs. String.includes vs. String.match
RegEx.test vs. String.includes x 2
Comments
Confirm delete:
Do you really want to delete benchmark?