Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex full match vs regex negated vs parseInt string test
(version: 1)
Comparing performance of:
regex full match check vs parseInt check vs regex negated check
Created:
7 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const testString = "1234567890" const regexFullMatch = /^\d+$/ const regexNegated = /[^\d]/
Tests:
regex full match check
const value = regexFullMatch.test(testString)
parseInt check
const value = Number.isNaN(parseInt(testString, 10))
regex negated check
const value = regexNegated.test(testString)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
regex full match check
parseInt check
regex negated check
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0
Browser/OS:
Firefox 144 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regex full match check
49575976.0 Ops/sec
parseInt check
2305327872.0 Ops/sec
regex negated check
51193584.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 7 months ago):
The benchmark provided tests the performance of three different methods for validating whether a string purely contains digits: using a full regular expression match, using a negated regular expression, and using `parseInt`. Each test case evaluates a different approach to ascertain the string's content. ### Test Cases Compared 1. **Regex Full Match Check** - **Expression**: `const value = regexFullMatch.test(testString)` - **Description**: This test uses a regular expression that checks if the entire string consists of digits (`/^\d+$/`). - **Pros**: - Clear intent as it directly checks for a full match against the pattern. - Easy to understand and modify for different matching patterns. - **Cons**: - Can be slower than pure numerical checks for longer strings due to overhead from regular expression processing. 2. **parseInt Check** - **Expression**: `const value = Number.isNaN(parseInt(testString, 10))` - **Description**: This method attempts to convert the string into an integer and checks if it results in `NaN` (not a number). If `parseInt` successfully parses the string to an integer, it returns that number; otherwise, it returns `NaN`. - **Pros**: - Generally faster than regex match since `parseInt` is a built-in function optimized for numerical parsing. - Effective for checking whole numbers without needing to form complex patterns. - **Cons**: - May lead to false positives if strings contain valid numbers but are formatted incorrectly (e.g., whitespace or delimiters). - Not as transparent for the goal of checking against specific character types. 3. **Regex Negated Check** - **Expression**: `const value = regexNegated.test(testString)` - **Description**: This test uses a negated regular expression (`/[^\\d]/`) to check for any non-digit characters in the string. - **Pros**: - More expressive in verifying the absence of unwanted characters. - Can be useful if you need to know that the string is either valid or invalid based on specific characters. - **Cons**: - Slower than other approaches due to regex overhead. - Requires careful keeping of the negated logic which may introduce complexity. ### Benchmark Results Overview - The results indicate that `parseInt` performed significantly faster than both regex tests, with an execution rate of approximately 2.3 billion executions per second compared to ~51 million for the negated regex and ~49 million for the full match regex. This shows a substantial performance advantage for numerical parsing via `parseInt`. ### Summary of Alternatives and Considerations 1. **String Methods**: One may also consider using built-in string methods, like `String.prototype.match()`, to return an array of matches for digit checks, but this would also involve regex and potentially bear the same performance overhead. 2. **Custom Iterative Checks**: For absolute performance, looping through the string characters and checking if each character is a digit using a simple comparison may yield better performance in very performance-critical cases. 3. **Use Cases**: - If clarity is necessary in understanding the code's intent, regex can be beneficial. - For performance-critical scenarios where numerous checks are required, leveraging `parseInt` or custom methods may be more suitable. In general, the choice between these approaches will depend on the specific needs of the application, balancing between readability, maintainability, and performance.
Related benchmarks:
regex vs length
.test vs .match vs modulo(string)
regex.test vs regex.match
isNaN vs regex test for stringify number check
regexp vs func
regexp vs func 2
parseFloat isNaN vs RegEx parseFloat
parseFloat isNaN vs RegEx parseFloat vs Number isNaN
regex vs parseInt string test
Comments
Confirm delete:
Do you really want to delete benchmark?