UNPKG

ts-time-utils

Version:

A comprehensive TypeScript utility library for time, dates, durations, and calendar operations with full tree-shaking support

103 lines 3.16 kB
/** * Async time utilities for delays, timeouts, and performance */ /** * Sleep for a specified number of milliseconds * @param ms - milliseconds to sleep */ export declare function sleep(ms: number): Promise<void>; /** * Add a timeout to any promise * @param promise - promise to add timeout to * @param ms - timeout in milliseconds * @param timeoutMessage - optional timeout error message */ export declare function timeout<T>(promise: Promise<T>, ms: number, timeoutMessage?: string): Promise<T>; /** * Debounce function - delays execution until after delay has passed since last call * @param fn - function to debounce * @param delay - delay in milliseconds */ export declare function debounce<T extends (...args: any[]) => any>(fn: T, delay: number): (...args: Parameters<T>) => void; /** * Throttle function - limits execution to once per delay period * @param fn - function to throttle * @param delay - delay in milliseconds */ export declare function throttle<T extends (...args: any[]) => any>(fn: T, delay: number): (...args: Parameters<T>) => void; /** * Retry a promise-returning function with exponential backoff * @param fn - function that returns a promise * @param maxAttempts - maximum number of attempts * @param baseDelay - base delay in milliseconds * @param maxDelay - maximum delay in milliseconds */ export declare function retry<T>(fn: () => Promise<T>, maxAttempts?: number, baseDelay?: number, maxDelay?: number): Promise<T>; /** * Stopwatch for measuring elapsed time */ export declare class Stopwatch { private startTime; private endTime; private pausedTime; private pauseStart; /** * Start the stopwatch */ start(): void; /** * Stop the stopwatch */ stop(): number; /** * Pause the stopwatch */ pause(): void; /** * Resume the stopwatch */ resume(): void; /** * Reset the stopwatch */ reset(): void; /** * Get elapsed time without stopping */ getElapsed(): number; /** * Check if stopwatch is running */ isRunning(): boolean; /** * Check if stopwatch is paused */ isPaused(): boolean; } /** * Create a new stopwatch instance */ export declare function createStopwatch(): Stopwatch; /** * Measure execution time of a synchronous function * @param fn - function to measure * @returns tuple of [result, elapsed time in ms] */ export declare function measureTime<T>(fn: () => T): [T, number]; /** * Measure execution time of an asynchronous function * @param fn - async function to measure * @returns promise that resolves to tuple of [result, elapsed time in ms] */ export declare function measureAsync<T>(fn: () => Promise<T>): Promise<[T, number]>; /** * Performance measurement and async utilities */ import type { BenchmarkResult } from './types.js'; /** * Benchmark a function by running it multiple times * @param fn - function to benchmark * @param iterations - number of iterations to run */ export declare function benchmark(fn: () => void, iterations?: number): BenchmarkResult; //# sourceMappingURL=performance.d.ts.map