@samatawy/rules
    Preparing search index...

    Class Stopwatch

    A utility class for measuring and logging the performance of code blocks, including execution time and memory usage. It can be used to create checkpoints and laps, allowing for detailed performance analysis. This class will automatically detect the environment (Node.js or browser) to provide relevant metrics.

    Index

    Constructors

    Methods

    • Start a new performance logger with the given log level and label. Nothing will be written to the log until the end method is called.

      Parameters

      • level: LogLevel

        the log level to use for logging performance data.

      • label: string

        a descriptive label for the performance measurement (to be shown in logs).

      Returns Stopwatch

      a new instance of Stopwatch.

    • Create a checkpoint for the current performance measurement, capturing the duration and memory usage since the last start or lap. This can be used for example, to find out which part of a process is taking the most time or memory, or to detect peak heap usage during a process (before the garbage collector cleans up after a scope exits). This does not write to a log by itself. N.B. Checkpoints differ from laps in that they do not reset any values (cumulative).

      Parameters

      • Optionallabel: string

      Returns StopwatchData

      an object containing the duration, heap delta, and a formatted message.

    • Create a lap for the current performance measurement, capturing the duration and memory usage since the last start or lap. This can be used to measure intermediate steps within a process. This does not write to a log by itself. N.B. Laps differ from checkpoints in that they reset all values (clean slate).

      Parameters

      • Optionallabel: string

      Returns StopwatchData

      an object containing the duration, heap delta, and a formatted message.

    • End the performance measurement and log the final results, including total duration and memory usage change, along with any provided metadata. This actually writes to a log (through Logger) with the specified log level and includes the label for context. The metadata parameter can be used to include additional contextual information in the logs, such as input sizes, configuration options, or any other relevant data that can help in analyzing the performance results.

      Parameters

      • Optionallabel: string
      • Optionalmetadata: Record<string, any>

        an object containing additional contextual information to be included in the performance log.

      Returns StopwatchData

      an object containing the duration, heap delta, and a formatted message.