Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reuse Regex? RegEx.test vs. String.match vs. String.search
(version: 0)
Comparing performance of:
RegEx.test vs String.match vs String.search
Created:
3 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var string = "Hello world!"; var regex = /Hello/;
Tests:
RegEx.test
regex.test(/Hello/);
String.match
string.match(regex);
String.search
string.search(regex)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
RegEx.test
String.match
String.search
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
15 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36
Browser/OS:
Chrome 150 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
String.search
59.0M/s
String.match
42.3M/s
RegEx.test
7.9M/s
View exact numbers
Test name
Executions per second
✓
String.search
59,012,544 Ops/sec
String.match
42,340,292 Ops/sec
RegEx.test
7,941,986 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. The benchmark is comparing three different approaches to search for a regex pattern in a string: 1. `String.search(regex)`: This method returns the index of the first occurrence of the pattern in the string. If no match is found, it returns -1. 2. `string.match(regex)`: This method returns an array of matches if the pattern is found anywhere in the string. If no match is found, it returns null. 3. `regex.test(string)`: This method tests whether the regular expression can be applied to a given string. It returns true if the pattern can be applied, false otherwise. Now, let's discuss the pros and cons of each approach: * **String.search(regex)**: + Pros: Fast and efficient when a single match is expected. + Cons: Returns an index value (-1 if no match), which may not be useful for all use cases. * **string.match(regex)**: + Pros: Can return multiple matches, making it suitable for finding all occurrences of the pattern. + Cons: Slower than `String.search(regex)` and may be less efficient when a single match is expected. * **regex.test(string)**: + Pros: More flexible and powerful than `String.search(regex)` and `string.match(regex)`, as it can test whether the regex can be applied to any part of the string. + Cons: May be slower than the other two approaches, especially for complex regex patterns. In this benchmark, we have three individual test cases: * **RegEx.test**: Tests `regex.test(string)` on a single input string. * **String.match**: Tests `string.match(regex)` on a single input string. * **String.search**: Tests `String.search(regex)` on a single input string. The latest benchmark result shows that: * `String.search` is the fastest approach, with approximately 4.2 million executions per second on Opera Mobile 74 running Android 12. * `string.match` is slower than `String.search`, but still relatively fast, with around 3.7 million executions per second. * `regex.test` is the slowest approach, with approximately 1.6 million executions per second. As for library usage, none of these approaches rely on any external libraries. There are no special JavaScript features or syntax used in this benchmark. If you're interested in exploring other alternatives, here are a few: * ** RegExp.prototype.exec()**: Similar to `String.search(regex)`, but can be more flexible and powerful when dealing with complex regex patterns. * ** RegExp.prototype.test()**: Similar to `regex.test(string)`, but can also be used as a callback function to test the regex against multiple strings. * ** String.prototype.replace()**: Can be used to replace occurrences of a pattern in a string, but may not be as efficient as the other approaches. Keep in mind that these alternatives may have different performance characteristics and use cases, so it's essential to evaluate them based on your specific requirements.
Related benchmarks
RegEx.test() vs String.search()
RegEx.test vs. String.includes vs. String.match vs String.match(regex) for starting string
RegEx.test vs. String.match vs. String.search
Comments
Confirm delete:
Do you really want to delete benchmark?