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 |
// 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)