1. /**
  2. * Measures the performance of text validation checks.
  3. *
  4. * @param {string} text The text to validate.
  5. * @param {function} validateFunction The validation function to test. Should return true if valid, false otherwise.
  6. * @param {object} options Optional settings.
  7. * @param {boolean} options.dryRun Whether to only measure and not execute the validation. Defaults to false.
  8. * @returns {object} An object containing the performance results.
  9. */
  10. function measureValidationPerformance(text, validateFunction, options = {}) {
  11. const { dryRun = false } = options;
  12. const startTime = performance.now();
  13. if (!dryRun) {
  14. validateFunction(text); // Execute the validation
  15. }
  16. const endTime = performance.now();
  17. const executionTime = endTime - startTime;
  18. return {
  19. executionTime: executionTime, // in milliseconds
  20. valid: validateFunction(text), // Result of the validation function
  21. };
  22. }
  23. // Example Usage (for testing):
  24. // function exampleValidate(text) {
  25. // return text.length > 5 && text.match(/[a-zA-Z]/); // Example validation
  26. // }
  27. // const textToValidate = "Hello World!";
  28. // const results = measureValidationPerformance(textToValidate, exampleValidate);
  29. // console.log(results);

Add your comment