UNPKG

@mui/internal-docs-infra

Version:

MUI Infra - internal documentation creation tools.

61 lines (59 loc) 2.17 kB
export function logPerformance(entry, notableMs, showWrapperMeasures, filterContext) { if (entry.duration < notableMs) { return; } // If filterContext is provided, only log entries that include it if (filterContext && !entry.name.includes(filterContext)) { return; } let delim = '-'; let message = entry.name; if (message.startsWith('| ')) { if (!showWrapperMeasures) { return; } delim = '|'; message = message.slice(2); } const duration = Math.round(entry.duration).toString().padStart(4, ' '); console.warn(`${duration}ms ${delim} ${message}`); } export function createPerformanceLogger(notableMs, showWrapperMeasures, filterContext) { const performanceLogger = list => { for (const entry of list.getEntries()) { logPerformance(entry, notableMs, showWrapperMeasures, filterContext); } }; return performanceLogger; } export function nameMark(functionName, event, context, wrapper = false) { return `${wrapper ? '| ' : ''}${functionName} ${wrapper ? '|' : '-'} ${event} - ${context.join(' - ')}`; } /** * Helper to create a performance mark and measure in one call. * * @param startMark - Optional start mark name for the measure (typically currentMark) * @param names - Object with mark and measure names * @param context - Array of context strings [functionName, ...context] * @param wrapper - Whether this is a wrapper measure (uses pipe delimiter) * @returns The mark name that was created * * @example * ```ts * currentMark = performanceMeasure( * currentMark, * { mark: 'processed', measure: 'processing' }, * [functionName, relativePath], * true, * ); * ``` */ export function performanceMeasure(startMark, names, context, wrapper = false) { const [functionName, ...restContext] = context; const prefix = 'prefix' in names && names.prefix ? `${names.prefix} ` : ''; const markName = nameMark(functionName, `${prefix}${names.mark}`, restContext, wrapper); const measureName = nameMark(functionName, `${prefix}${names.measure}`, restContext, wrapper); performance.mark(markName); performance.measure(measureName, startMark, markName); return markName; }