1. /**
  2. * Measures the performance of text blocks for hypothesis validation.
  3. *
  4. * @param {string} textBlock - The text block to measure.
  5. * @param {function} validationFunction - A function that validates the text block.
  6. * Should return true if the hypothesis is supported, false otherwise.
  7. * @param {number} iterations - The number of times to run the validation. Defaults to 100.
  8. * @returns {object} An object containing the average validation time in milliseconds and the success rate.
  9. */
  10. function measureTextBlockPerformance(textBlock, validationFunction, iterations = 100) {
  11. const startTime = performance.now();
  12. let successCount = 0;
  13. for (let i = 0; i < iterations; i++) {
  14. validationFunction(textBlock); // Run the validation function
  15. if (validationFunction(textBlock)) {
  16. successCount++;
  17. }
  18. }
  19. const endTime = performance.now();
  20. const elapsedTime = endTime - startTime;
  21. const averageTime = elapsedTime / iterations;
  22. const successRate = successCount / iterations;
  23. return {
  24. averageTime: averageTime,
  25. successRate: successRate,
  26. };
  27. }
  28. //Example Usage (for testing)
  29. // function exampleValidation(text) {
  30. // // Replace this with your actual validation logic
  31. // return text.length > 10;
  32. // }
  33. //const results = measureTextBlockPerformance("This is a long text.", exampleValidation, 1000);
  34. //console.log(results);

Add your comment