developer-utils/measure-performance.mjs

/**
 * @module developer-utils
 */

/**
 * Measures the execution time of a given function by running it multiple times.
 * Useful for micro-benchmarking and performance comparison.
 * 
 *
 * @param {Function} fn - The function to benchmark.
 * @param {number} [steps=1e8] - The number of iterations to run the function.
 * @param {string} [memo='noop'] - A descriptive label for the benchmark result.
 * @returns {Object} An object containing the `memo` and the `duration` (in milliseconds).
 * @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4LAUwIYGcCuM0AKaMAZrGBgHYDGaCAvgiTFGAgEQACA5gNY7UwIYgC8A9DjggANlnYBuYMHTY8hYmRgUaaRUA|▶ Try it live in Instacode}
 */
export const measurePerformance = (fn, steps = 1e8, memo = 'noop') => {
  const t1 = Date.now();
  while (steps--) {
    fn();
  }
  const t2 = Date.now();
  
  return {
    memo,
    duration: t2 - t1
  };
};