CmpStr - v3.2.1
    Preparing search index...

    Profiler class for measuring execution time and memory usage of functions.

    Index

    Constructors

    • Private constructor to enforce singleton pattern. Initializes the store for profiler entries.

      Parameters

      • Optionalactive: boolean = false

        Optional parameter to enable the profiler

      Returns Profiler

    Properties

    active: boolean = false

    Optional parameter to enable the profiler

    memFn: () => number
    nowFn: () => number

    Pre-computed functions for time and memory retrieval

    services: ProfilerService<any> = ...

    Returns the services provided by the Profiler class. This allows for easy access to the profiler's methods.

    • An object containing methods to control the profiler
    store: Set<ProfilerEntry<any>> = ...

    Store for profiler entries

    totalMem: number = 0
    totalTime: number = 0

    Total time and memory consumption

    ENV: "unknown" | "browser" | "nodejs"

    Environment detection

    instance: Profiler

    Singleton instance

    Methods

    • Resets the profiler by clearing the store, total time and memory consumption. This method is useful for starting a new profiling session.

      Returns void

    • Disables the profiler. Sets the active state to false, preventing further profiling.

      Returns void

    • Enables the profiler. Sets the active state to true, allowing profiling to occur.

      Returns void

    • Retrieves the total time and memory consumption recorded by the profiler.

      Returns { mem: number; time: number }

      • An object containing total time and memory usage
    • Gets the current memory usage based on the environment.

      Uses process.memoryUsage().heapUsed for Node.js, performance.memory.usedJSHeapSize for browsers, and returns 0 as a fallback.

      Returns number

      • Current memory usage in bytes
    • Gets the current time based on the environment.

      Uses process.hrtime.bigint() for Node.js, performance.now() for browsers, and Date.now() as a fallback.

      Returns number

      • Current time in milliseconds
    • Profiles a synchronous function by measuring its execution time and memory usage.

      Type Parameters

      • T

      Parameters

      • fn: () => T

        Function to be executed and profiled

      • meta: Record<string, any>

        Metadata to be associated with the profiling entry

      Returns T

      • The result of the executed function
    • Runs a synchronous function and profiles its execution time and memory usage. If the profiler is not active, it simply executes the function without profiling.

      Type Parameters

      • T

      Parameters

      • fn: () => T

        Function to be executed and profiled

      • meta: Record<string, any> = {}

        Metadata to be associated with the profiling entry

      Returns T

      • The result of the executed function
    • Runs an asynchronous function and profiles its execution time and memory usage. If the profiler is not active, it simply executes the function without profiling.

      Type Parameters

      • T

      Parameters

      • fn: () => Promise<T>

        Asynchronous function to be executed and profiled

      • meta: Record<string, any> = {}

        Metadata to be associated with the profiling entry

      Returns Promise<T>

      • A promise that resolves to the result of the executed function
    • Sets the environment based on the available global objects. Detects if running in Node.js or browser and sets the ENV property accordingly.

      Returns void

    • Returns the singleton instance of the Profiler class. If the instance does not exist, it creates a new one.

      Parameters

      • Optionalenable: boolean

        Optional parameter to enable the profiler upon instantiation

      Returns Profiler

      • Singleton Profiler instance