Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.match vs. String.indexOf dg
(version: 0)
Comparing performance of:
RegEx.test vs String.includes vs String.match 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.match
string.match("Hello");
string.indexOf
-1 !== string.indexOf("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.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 dive into the benchmark. **What is tested?** The benchmark measures the performance of three string comparison methods: 1. `RegEx.test(string)`: Uses a regular expression to test if a given pattern matches at the beginning of a string. 2. `string.includes("Hello")`: Tests if a substring is present in a string using the `includes()` method. 3. `string.match("Hello")`: Attempts to match a regular expression against a string, returning an array if a match is found or null otherwise. 4. `string.indexOf("Hello")`: Searches for a specific value in a string and returns its position. **Options compared** The benchmark compares the performance of these four methods: * `RegEx.test(string)`: Uses a regular expression to test if a pattern matches at the beginning of the string. + Pros: Efficient when working with patterns that require exact matching. + Cons: May be slower than other methods for simple substring searches, as it requires compiling and executing a regex engine. * `string.includes("Hello")`: Tests if a substring is present in a string using the `includes()` method. + Pros: Fast and efficient for simple substring searches. Newer JavaScript engines (ES6+) have optimized this method. + Cons: May be slower than other methods that use explicit loop iteration or regexes. * `string.match("Hello")`: Attempts to match a regular expression against a string, returning an array if a match is found or null otherwise. + Pros: Can return multiple matches in a single operation. Suitable for parsing regular expressions. + Cons: May be slower than other methods that use explicit loop iteration or regexes. * `string.indexOf("Hello")`: Searches for a specific value in a string and returns its position. + Pros: Efficient for searching for a specific value within a large string. Optimized for ES5 browsers. + Cons: Returns -1 if the value is not found, which may be slower than methods that return null or throw an error. **Library usage** None of the test cases use any additional libraries beyond built-in JavaScript features. **Special JS feature/syntax** There are no special JS features or syntax used in this benchmark. All comparisons rely on standard JavaScript strings and regular expressions. **Alternatives** Other alternatives for string comparison methods include: * `string.localeCompare("Hello")`: Compares two strings lexicographically, ignoring case. * `string.startsWith("Hello")` / `string.endsWith("Hello")`: Tests if a string starts or ends with a specific value. * Custom loop-based implementations using `for` loops to iterate over the string characters. Keep in mind that the performance of these alternatives may vary depending on the specific use case and JavaScript engine.
Related benchmarks:
RegEx.test vs String.includes vs String.indexOf
RegEx.test vs. String.includes vs. String.indexOf
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
Comments
Confirm delete:
Do you really want to delete benchmark?