houser-js-utils
Version:
A comprehensive collection of TypeScript utility functions for common development tasks including array manipulation, string processing, date handling, random number generation, validation, and much more.
1 lines • 7.59 kB
Source Map (JSON)
{"version":3,"file":"PerformanceUtils.mjs","sources":["../src/PerformanceUtils.ts"],"sourcesContent":["/**\n * @module PerformanceUtils\n * @description A collection of utility functions for performance monitoring, timing, and optimization.\n * @example\n * ```typescript\n * import { PerformanceUtils } from 'houser-js-utils';\n *\n * // Measure execution time\n * const timer = PerformanceUtils.startTimer();\n * // ... some operation\n * const elapsed = PerformanceUtils.endTimer(timer);\n *\n * // Get memory usage\n * const memoryInfo = PerformanceUtils.getMemoryUsage();\n * ```\n */\n\nexport const PerformanceUtils = {\n /**\n * Clears all performance marks and measures\n */\n clearMarksAndMeasures(): void {\n performance.clearMarks();\n performance.clearMeasures();\n },\n\n /**\n * Creates a performance observer\n * @param callback - Function to call when performance entries are observed\n * @param entryTypes - Types of entries to observe\n * @returns Performance observer\n */\n createPerformanceObserver(\n callback: (entries: PerformanceObserverEntryList) => void,\n entryTypes: string[]\n ): PerformanceObserver {\n const observer = new PerformanceObserver(callback);\n observer.observe({ entryTypes });\n return observer;\n },\n\n /**\n * Gets the cumulative layout shift\n * @returns Promise resolving to CLS score\n */\n async getCumulativeLayoutShift(): Promise<number> {\n const cls = performance.getEntriesByType(\"layout-shift\");\n return cls.reduce((sum, entry) => sum + (entry as any).value, 0);\n },\n\n /**\n * Gets the DOM content loaded time\n * @returns Promise resolving to DOM content loaded time in milliseconds\n */\n async getDomContentLoadedTime(): Promise<number> {\n const navigation = performance.getEntriesByType(\n \"navigation\"\n )[0] as PerformanceNavigationTiming;\n return navigation.domContentLoadedEventEnd - navigation.startTime;\n },\n\n /**\n * Gets the first contentful paint time\n * @returns Promise resolving to FCP time in milliseconds\n */\n async getFirstContentfulPaint(): Promise<number> {\n const paint = performance.getEntriesByType(\"paint\");\n const fcp = paint.find((entry) => entry.name === \"first-contentful-paint\");\n return fcp ? fcp.startTime : 0;\n },\n\n /**\n * Gets the first input delay\n * @returns Promise resolving to FID time in milliseconds\n */\n async getFirstInputDelay(): Promise<number> {\n const fid = performance.getEntriesByType(\"first-input\");\n return fid.length > 0 ? fid[0].startTime : 0;\n },\n\n /**\n * Gets the largest contentful paint time\n * @returns Promise resolving to LCP time in milliseconds\n */\n async getLargestContentfulPaint(): Promise<number> {\n const lcp = performance.getEntriesByType(\"largest-contentful-paint\");\n return lcp.length > 0 ? lcp[lcp.length - 1].startTime : 0;\n },\n\n /**\n * Gets the page load time\n * @returns Promise resolving to page load time in milliseconds\n */\n async getPageLoadTime(): Promise<number> {\n const navigation = performance.getEntriesByType(\n \"navigation\"\n )[0] as PerformanceNavigationTiming;\n return navigation.loadEventEnd - navigation.startTime;\n },\n\n /**\n * Gets the time to first byte (TTFB)\n * @returns Promise resolving to TTFB in milliseconds\n */\n async getTimeToFirstByte(): Promise<number> {\n const navigation = performance.getEntriesByType(\n \"navigation\"\n )[0] as PerformanceNavigationTiming;\n return navigation.responseStart - navigation.requestStart;\n },\n\n /**\n * Creates a performance mark\n * @param name - Name of the mark\n */\n mark(name: string): void {\n performance.mark(name);\n },\n\n /**\n * Measures the execution time of a function\n * @param fn - Function to measure\n * @param iterations - Number of iterations to run\n * @returns Object containing execution time statistics\n */\n measureExecutionTime<T>(\n fn: () => T,\n iterations = 1\n ): { result: T; time: number; averageTime: number } {\n const start = performance.now();\n let result: T;\n\n for (let i = 0; i < iterations; i++) {\n result = fn();\n }\n\n const end = performance.now();\n const time = end - start;\n const averageTime = time / iterations;\n\n return { result: result!, time, averageTime };\n },\n\n /**\n * Measures the time between two marks\n * @param startMark - Name of the start mark\n * @param endMark - Name of the end mark\n * @returns Object containing measurement details\n */\n measure(startMark: string, endMark: string) {\n try {\n performance.measure(`${startMark}-${endMark}`, startMark, endMark);\n const entries = performance.getEntriesByName(`${startMark}-${endMark}`);\n return entries[entries.length - 1];\n } catch (error) {\n console.error(\"Error measuring performance:\", error);\n return null;\n }\n },\n\n /**\n * Throttles a function\n * @param fn - Function to throttle\n * @param limit - Time limit in milliseconds\n * @returns Throttled function\n */\n throttle<T extends (...args: any[]) => any>(\n fn: T,\n limit: number\n ): (...args: Parameters<T>) => void {\n let inThrottle: boolean;\n return (...args: Parameters<T>) => {\n if (!inThrottle) {\n fn(...args);\n inThrottle = true;\n setTimeout(() => (inThrottle = false), limit);\n }\n };\n },\n};\n"],"names":[],"mappings":"AAiBO,MAAM,mBAAmB;AAAA;AAAA;AAAA;AAAA,EAI9B,wBAA8B;AAC5B,gBAAY,WAAA;AACZ,gBAAY,cAAA;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,0BACE,UACA,YACqB;AACrB,UAAM,WAAW,IAAI,oBAAoB,QAAQ;AACjD,aAAS,QAAQ,EAAE,YAAY;AAC/B,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,2BAA4C;AAChD,UAAM,MAAM,YAAY,iBAAiB,cAAc;AACvD,WAAO,IAAI,OAAO,CAAC,KAAK,UAAU,MAAO,MAAc,OAAO,CAAC;AAAA,EACjE;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,0BAA2C;AAC/C,UAAM,aAAa,YAAY;AAAA,MAC7B;AAAA,IAAA,EACA,CAAC;AACH,WAAO,WAAW,2BAA2B,WAAW;AAAA,EAC1D;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,0BAA2C;AAC/C,UAAM,QAAQ,YAAY,iBAAiB,OAAO;AAClD,UAAM,MAAM,MAAM,KAAK,CAAC,UAAU,MAAM,SAAS,wBAAwB;AACzE,WAAO,MAAM,IAAI,YAAY;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,qBAAsC;AAC1C,UAAM,MAAM,YAAY,iBAAiB,aAAa;AACtD,WAAO,IAAI,SAAS,IAAI,IAAI,CAAC,EAAE,YAAY;AAAA,EAC7C;AAAA;AAAA;AAAA;AAAA;AAAA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