diff --git a/packages/utils/api-report.md b/packages/utils/api-report.md index e1379999c..fdf4cd9b4 100644 --- a/packages/utils/api-report.md +++ b/packages/utils/api-report.md @@ -176,6 +176,12 @@ export function mapObjectMapValues( [K in Key]: ValueAfter; }; +// @internal (undocumented) +export function measureCbDuration(name: string, cb: () => any): any; + +// @internal (undocumented) +export function measureDuration(_target: any, propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor; + // @public export class MediaHelpers { static getImageSize(blob: Blob): Promise<{ diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index b008b9753..598a26dd7 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -36,6 +36,7 @@ export { objectMapKeys, objectMapValues, } from './lib/object' +export { measureCbDuration, measureDuration } from './lib/perf' export { PngHelpers } from './lib/png' export { type IndexKey } from './lib/reordering/IndexKey' export { diff --git a/packages/utils/src/lib/perf.ts b/packages/utils/src/lib/perf.ts new file mode 100644 index 000000000..689f8640a --- /dev/null +++ b/packages/utils/src/lib/perf.ts @@ -0,0 +1,22 @@ +/** @internal */ +export function measureCbDuration(name: string, cb: () => any) { + const now = performance.now() + const result = cb() + // eslint-disable-next-line no-console + console.log(`${name} took`, performance.now() - now, 'ms') + return result +} + +/** @internal */ +export function measureDuration(_target: any, propertyKey: string, descriptor: PropertyDescriptor) { + const originalMethod = descriptor.value + descriptor.value = function (...args: any[]) { + const start = performance.now() + const result = originalMethod.apply(this, args) + const end = performance.now() + // eslint-disable-next-line no-console + console.log(`${propertyKey} took ${end - start}ms `) + return result + } + return descriptor +}