UNPKG

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.

144 lines (143 loc) 4.34 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const PerformanceUtils = { /** * Clears all performance marks and measures */ clearMarksAndMeasures() { performance.clearMarks(); performance.clearMeasures(); }, /** * Creates a performance observer * @param callback - Function to call when performance entries are observed * @param entryTypes - Types of entries to observe * @returns Performance observer */ createPerformanceObserver(callback, entryTypes) { const observer = new PerformanceObserver(callback); observer.observe({ entryTypes }); return observer; }, /** * Gets the cumulative layout shift * @returns Promise resolving to CLS score */ async getCumulativeLayoutShift() { const cls = performance.getEntriesByType("layout-shift"); return cls.reduce((sum, entry) => sum + entry.value, 0); }, /** * Gets the DOM content loaded time * @returns Promise resolving to DOM content loaded time in milliseconds */ async getDomContentLoadedTime() { const navigation = performance.getEntriesByType( "navigation" )[0]; return navigation.domContentLoadedEventEnd - navigation.startTime; }, /** * Gets the first contentful paint time * @returns Promise resolving to FCP time in milliseconds */ async getFirstContentfulPaint() { const paint = performance.getEntriesByType("paint"); const fcp = paint.find((entry) => entry.name === "first-contentful-paint"); return fcp ? fcp.startTime : 0; }, /** * Gets the first input delay * @returns Promise resolving to FID time in milliseconds */ async getFirstInputDelay() { const fid = performance.getEntriesByType("first-input"); return fid.length > 0 ? fid[0].startTime : 0; }, /** * Gets the largest contentful paint time * @returns Promise resolving to LCP time in milliseconds */ async getLargestContentfulPaint() { const lcp = performance.getEntriesByType("largest-contentful-paint"); return lcp.length > 0 ? lcp[lcp.length - 1].startTime : 0; }, /** * Gets the page load time * @returns Promise resolving to page load time in milliseconds */ async getPageLoadTime() { const navigation = performance.getEntriesByType( "navigation" )[0]; return navigation.loadEventEnd - navigation.startTime; }, /** * Gets the time to first byte (TTFB) * @returns Promise resolving to TTFB in milliseconds */ async getTimeToFirstByte() { const navigation = performance.getEntriesByType( "navigation" )[0]; return navigation.responseStart - navigation.requestStart; }, /** * Creates a performance mark * @param name - Name of the mark */ mark(name) { performance.mark(name); }, /** * Measures the execution time of a function * @param fn - Function to measure * @param iterations - Number of iterations to run * @returns Object containing execution time statistics */ measureExecutionTime(fn, iterations = 1) { const start = performance.now(); let result; for (let i = 0; i < iterations; i++) { result = fn(); } const end = performance.now(); const time = end - start; const averageTime = time / iterations; return { result, time, averageTime }; }, /** * Measures the time between two marks * @param startMark - Name of the start mark * @param endMark - Name of the end mark * @returns Object containing measurement details */ measure(startMark, endMark) { try { performance.measure(`${startMark}-${endMark}`, startMark, endMark); const entries = performance.getEntriesByName(`${startMark}-${endMark}`); return entries[entries.length - 1]; } catch (error) { console.error("Error measuring performance:", error); return null; } }, /** * Throttles a function * @param fn - Function to throttle * @param limit - Time limit in milliseconds * @returns Throttled function */ throttle(fn, limit) { let inThrottle; return (...args) => { if (!inThrottle) { fn(...args); inThrottle = true; setTimeout(() => inThrottle = false, limit); } }; } }; exports.PerformanceUtils = PerformanceUtils; //# sourceMappingURL=PerformanceUtils.js.map