Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Class Action Testing
(version: 1)
Comparing performance of:
RegEx.test vs String.includes vs String.match
Created:
9 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "Sarepta Therapeutics (SRPT) Announces Restructuring and ELEVIDYS Black Box Warning, EMA Recommends Refusal of Marketing Authorization, Securities Class Action Pending – Hagens Berman!"; var regex = /Class Action/;
Tests:
RegEx.test
regex.test(string);
String.includes
string.includes("Class Action");
String.match
string.match("Class Action");
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:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
33210616.0 Ops/sec
String.includes
151586720.0 Ops/sec
String.match
7035095.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 9 months ago):
The benchmark titled "Class Action Testing" evaluates the performance of different JavaScript string search methods when looking for the substring "Class Action" in a predefined string derived from a news article. ### Options Compared: 1. **RegExp.test()**: This method tests whether a regular expression (regex) matches a portion of the string. In this case, it uses the regex `/Class Action/`. 2. **String.includes()**: This method checks if a specific substring is present within a string and returns a boolean value (`true` or `false`). 3. **String.match()**: This method retrieves the matches of a string against a regex. It returns an array containing the matches or `null` if there are no matches. ### Pros and Cons of Approaches: 1. **RegExp.test()** - **Pros**: - Flexible: As a regular expression, it can support complex patterns beyond simple substrings. - Returns a boolean, which is generally faster for existence checks. - **Cons**: - For simple substring matches, it may be overkill and could introduce some performance overhead due to regex parsing and compilation. 2. **String.includes()** - **Pros**: - Simplicity: Easy to read and understand. Clearly describes the intent of the code (checking for a substring presence). - Generally optimized for performance when checking for a substring. - **Cons**: - Limited to simple substring searches; cannot handle more complex search criteria (like patterns with wildcards). 3. **String.match()** - **Pros**: - Versatile: Can retrieve all matches in the string when working with more complex patterns. - Returns match details, aiding in further manipulations. - **Cons**: - Typically has higher overhead when searching for simple substrings compared to includes or test, especially if the regex is more complex. - Outputs an array or null, which requires additional handling in code when just checking for existence. ### Benchmark Results: - The results show that `String.includes()` is the fastest method with around **151.59 million executions per second**. - Next is `RegExp.test()` at approximately **33.21 million executions per second**. - `String.match()` is the slowest, with about **7.03 million executions per second**. This reflects its heavier overhead due to returning match data. ### Considerations: - When performance is critical and you are only checking for substring presence, `String.includes()` is the preferred methodology based on benchmark results. - If the need arises for more complex searches, then using regex might be warranted, but consider the performance trade-off. - It is valuable to maintain readability in your code. For simple cases, `String.includes()` is not only fast but also makes the intent clear to anyone who might read the code later. ### Alternatives: - **String.startsWith() and String.endsWith()**: These methods could also be considered for checking if the string starts or ends with specific substrings, but they don't directly compete with the tested methods in terms of searching for substrings within the middle of a string. - Custom implementations: Depending on specific needs, one could create a loop-based search algorithm, but performance is generally worse than the built-in string methods unless dealing with unique data types or requirements. Overall, this benchmark facilitates understanding the nuances in performance for common string searching methodologies in JavaScript and serves as useful guidance for optimal string handling in web development.
Related benchmarks:
Regex check
RegEx.test vs. String.includes vs. String.match v2
eredfgfgsfgd4erfsdfgdf
RegEx.test vs. String.includes vs. String.match 1
RegEx.exec vs String.match2
RegEx.test vs. String.includes vs1531515. String.match
We test stuff!
search @ in email
RegEx.test vs. String.includes vs. String.match pr
Comments
Confirm delete:
Do you really want to delete benchmark?