// Test description: Comparing regex validation vs try-catch with BigInt for numeric string validation
// Setup
const validTokenId = "12345678901234567890"; // Valid numeric string
const invalidTokenId = "123abc456"; // Invalid numeric string containing letters
// Test function for regex validation
function validateWithRegex(tokenId) {
return /^\d+$/.test(tokenId);
}
// Test function for try-catch validation
function validateWithTryCatch(tokenId) {
try {
BigInt(tokenId);
return true;
} catch {
return false;
}
}
// Benchmark suite
suite('String Numeric Validation', function() {
// Regex with valid input
benchmark('Regex validation - Valid input', function() {
return validateWithRegex(validTokenId);
});
// Regex with invalid input
benchmark('Regex validation - Invalid input', function() {
return validateWithRegex(invalidTokenId);
});
// Try-catch with valid input
benchmark('Try-catch validation - Valid input', function() {
return validateWithTryCatch(validTokenId);
});
// Try-catch with invalid input
benchmark('Try-catch validation - Invalid input', function() {
return validateWithTryCatch(invalidTokenId);
});
});
validateWithRegex(validTokenId)
validateWithRegex(invalidTokenId)
validateWithTryCatch(validTokenId)
validateWithTryCatch(invalidTokenId)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
TestRegex validation - Valid input | |
Regex validation - Invalid input | |
Try-catch validation - Valid input | |
Try-catch validation - Invalid input |
Test name | Executions per second |
---|---|
TestRegex validation - Valid input | 26010314.0 Ops/sec |
Regex validation - Invalid input | 43248424.0 Ops/sec |
Try-catch validation - Valid input | 11689074.0 Ops/sec |
Try-catch validation - Invalid input | 102001.5 Ops/sec |
The benchmark provided compares two methods for validating numeric strings in JavaScript: using a regular expression (regex) and using a try-catch block combined with the BigInt
constructor. It measures how well each method performs under different input conditions (valid and invalid numeric strings).
Regex Validation:
validateWithRegex(tokenId)
uses the regex pattern /^\d+$/
, which tests if the input string contains only digits from start to end.validTokenId
: a string that represents a valid numeric value "12345678901234567890"
.invalidTokenId
: a string containing non-numeric characters "123abc456"
.Try-Catch Validation:
validateWithTryCatch(tokenId)
attempts to create a BigInt
from the input string. If the string is not a valid numeric value, it throws an error, which is caught in the catch block.BigInt
: This special JavaScript feature allows for the representation of integers larger than Number.MAX_SAFE_INTEGER
(2^53 - 1). Using BigInt
is significant for applications requiring arithmetic with very large integers.The benchmark includes four test cases:
Regex validation with valid input: Measures effectiveness and performance when the input string is valid.
true
.Regex validation with invalid input: Tests how the regex behaves with non-numeric input.
false
.Try-catch validation with valid input: Assesses performance when BigInt
should successfully convert the valid numeric string.
true
.Try-catch validation with invalid input: Tests how BigInt
behaves with invalid numeric strings.
false
.The benchmark results show the number of executions per second for each test case:
Regex Validation:
Pros:
Cons:
Try-Catch Validation:
Pros:
Cons:
TypeScript or Runtime-Type-checking Libraries: Instead of using raw try-catch
or regex, libraries like zod
or Joi
can offer more sophisticated validation, albeit with potential performance costs.
Native Number Parsing: Utilizing parseFloat
or parseInt
could also be a valid option, albeit they have limitations (e.g., they do not handle large integers as accurately as BigInt
).
The benchmark effectively illustrates the performance differences between using regex and try-catch with BigInt
for validating numeric strings. While regex is faster and simple for basic validation, BigInt
provides robust handling for large integers at the cost of performance, particularly in an invalid input scenario. Understanding these trade-offs allows developers to choose the most appropriate method depending on their application's requirements.