Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Logs - RegEx.test vs. String.includes vs. String.match
(version: 3)
Comparing performance of:
match vs includes vs regex
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const regex = /TF_LOG|(?:Creating|Modifying|Destroying)\.\.\./; const string = "module.helm_agent.helm_release.agent: Still modifying... [id=agent-bors, 10s elapsed]";
Tests:
match
function isRelevantLogEntryStrMatch(logEntry) { return ( logEntry.match('TF_LOG') || logEntry.match('Creating...') || logEntry.match('Modifying...') || logEntry.match('Destroying...') ); } isRelevantLogEntryStrMatch(string);
includes
function isRelevantLogEntryIncludes(logEntry) { return ( logEntry.includes('TF_LOG') || logEntry.includes('Creating...') || logEntry.includes('Modifying...') || logEntry.includes('Destroying...') ); } isRelevantLogEntryIncludes(string);
regex
function isRelevantLogEntryRegex(logEntry) { return regex.test(logEntry); } isRelevantLogEntryRegex(string);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
match
includes
regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
match
1956079.6 Ops/sec
includes
184235808.0 Ops/sec
regex
20448860.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark compares three different approaches for checking whether a log entry contains specific keywords, namely: `String.match`, `String.includes`, and `RegExp.test`. Each method has its own implementation and performance characteristics. ### Options Compared 1. **String.match**: - **Implementation**: This method uses regular expressions (regex) to search for matches in the string. - **Test Function**: ```javascript function isRelevantLogEntryStrMatch(logEntry) { return ( logEntry.match('TF_LOG') || logEntry.match('Creating...') || logEntry.match('Modifying...') || logEntry.match('Destroying...') ); } ``` 2. **String.includes**: - **Implementation**: This method checks if a string contains a specified substring. - **Test Function**: ```javascript function isRelevantLogEntryIncludes(logEntry) { return ( logEntry.includes('TF_LOG') || logEntry.includes('Creating...') || logEntry.includes('Modifying...') || logEntry.includes('Destroying...') ); } ``` 3. **RegExp.test** (using the pre-defined regex): - **Implementation**: This method tests if the regex matches the string. - **Test Function**: ```javascript function isRelevantLogEntryRegex(logEntry) { return regex.test(logEntry); } ``` ### Performance Results The benchmark results show that: - The **String.includes** method is the fastest, with a performance of approximately **184 million executions per second**. - The **RegExp.test** method follows, with about **20 million executions per second**. - The **String.match** approach is the slowest, with about **1.96 million executions per second**. ### Pros and Cons - **String.match**: - **Pros**: Flexible due to regex capabilities, can handle complex patterns. - **Cons**: Generally slower because of regex compilation and evaluation overhead. - **String.includes**: - **Pros**: Fast, straightforward, and easy to understand. Ideal for simple substring checks. - **Cons**: Limited to exact substrings; cannot handle complex matching patterns. - **RegExp.test**: - **Pros**: Maintains the flexibility of regex but can be faster than `String.match` for simple checks, depending on regex complexity. - **Cons**: Still has performance overhead compared to `String.includes`. ### Considerations - When performance is critical and the checks are for specific substrings, `String.includes` is the best choice due to its speed and simplicity. - If more complex pattern matching is required, `RegExp.test` might be preferable to `String.match`, especially if reused regex patterns can be defined, thereby avoiding re-compilation on each call. - The overhead of regular expressions may not be warranted for simple checks, so understanding the specific requirements of the task at hand is vital in selecting the best approach. ### Alternatives Other alternatives for checking the presence of substrings could include: - **Manual String Comparison**: For specific cases where the keywords are known and few, manual substrings could potentially process faster than both `includes` and regex methods. - **Using a Set or Trie Data Structure**: For more complex scenarios and larger collections of keywords, a Set or Trie could be used for effective membership testing. Overall, the choice among these options should be guided by both performance considerations and the complexity of the checks required in the context of your application.
Related benchmarks:
Console.log
Console.log vs Empty log
includes v regex
plus or regexp2
console.log test
compare regex pattern
+= vs templateString ...
lodash _.includes vs regex
Bzy INCLUDES, EXEC, MATCH Regex test
Comments
Confirm delete:
Do you really want to delete benchmark?