/**
* Measures the performance of text validation checks.
*
* @param {string} text The text to validate.
* @param {function} validateFunction The validation function to test. Should return true if valid, false otherwise.
* @param {object} options Optional settings.
* @param {boolean} options.dryRun Whether to only measure and not execute the validation. Defaults to false.
* @returns {object} An object containing the performance results.
*/
function measureValidationPerformance(text, validateFunction, options = {}) {
const { dryRun = false } = options;
const startTime = performance.now();
if (!dryRun) {
validateFunction(text); // Execute the validation
}
const endTime = performance.now();
const executionTime = endTime - startTime;
return {
executionTime: executionTime, // in milliseconds
valid: validateFunction(text), // Result of the validation function
};
}
// Example Usage (for testing):
// function exampleValidate(text) {
// return text.length > 5 && text.match(/[a-zA-Z]/); // Example validation
// }
// const textToValidate = "Hello World!";
// const results = measureValidationPerformance(textToValidate, exampleValidate);
// console.log(results);
Add your comment