measure-fn 3.10.1 → 3.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.ts +10 -8
  2. package/package.json +2 -2
package/index.ts CHANGED
@@ -211,6 +211,7 @@ const defaultLogger = (event: MeasureEvent, prefix?: string) => {
211
211
 
212
212
  export type MeasureFn = {
213
213
  <U>(label: string | object, fn: () => Promise<U>): Promise<U | null>;
214
+ <U>(label: string | object, fn: (m: MeasureFn, ms: MeasureSyncFn) => Promise<U>): Promise<U | null>;
214
215
  <U>(label: string | object, fn: (m: MeasureFn) => Promise<U>): Promise<U | null>;
215
216
  (label: string | object): Promise<null>;
216
217
  };
@@ -283,7 +284,7 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }, scop
283
284
  let _lastError: unknown = null;
284
285
 
285
286
  const _measureInternal = async <U>(
286
- fnInternal: (measure: MeasureFn) => Promise<U>,
287
+ fnInternal: (measure: MeasureFn, measureSync: MeasureSyncFn) => Promise<U>,
287
288
  actionInternal: string | object,
288
289
  parentIdChain: (string | number)[],
289
290
  depth: number,
@@ -298,8 +299,8 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }, scop
298
299
  const localMaxLen = extractMaxResultLength(actionInternal);
299
300
  const effectiveMaxLen = localMaxLen ?? inheritedMaxLen;
300
301
 
301
- const currentId = toAlpha(Number(parentIdChain.pop() ?? 0));
302
- const fullIdChain: string[] = [...parentIdChain.map(String), currentId];
302
+ const currentId = toAlpha(Number(parentIdChain.pop() ?? 0) as number);
303
+ const fullIdChain: string[] = [...parentIdChain.map(v => String(v)) as string[], currentId];
303
304
  const idStr = fullIdChain.join('-');
304
305
 
305
306
  emit({
@@ -311,18 +312,19 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }, scop
311
312
  }, prefix);
312
313
 
313
314
  const measureForNextLevel = createNestedResolver(true, fullIdChain, childCounterRef, depth, _measureInternal, prefix, effectiveMaxLen);
315
+ const measureSyncForNextLevel = createNestedResolver(false, fullIdChain, childCounterRef, depth, _measureInternalSync, prefix, effectiveMaxLen);
314
316
 
315
317
  try {
316
318
  let result: U;
317
319
  if (timeout && timeout > 0) {
318
320
  result = await Promise.race([
319
- fnInternal(measureForNextLevel as MeasureFn),
321
+ fnInternal(measureForNextLevel as MeasureFn, measureSyncForNextLevel as MeasureSyncFn),
320
322
  new Promise<never>((_, reject) =>
321
323
  setTimeout(() => reject(new Error(`Timeout (${formatDuration(timeout)})`)), timeout)
322
324
  ),
323
325
  ]);
324
326
  } else {
325
- result = await fnInternal(measureForNextLevel as MeasureFn);
327
+ result = await fnInternal(measureForNextLevel as MeasureFn, measureSyncForNextLevel as MeasureSyncFn);
326
328
  }
327
329
  const duration = performance.now() - start;
328
330
  emit({ type: 'success', id: idStr, label, depth, duration, result, budget, maxResultLength: effectiveMaxLen }, prefix);
@@ -360,8 +362,8 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }, scop
360
362
  const localMaxLen = extractMaxResultLength(actionInternal);
361
363
  const effectiveMaxLen = localMaxLen ?? inheritedMaxLen;
362
364
 
363
- const currentId = toAlpha(Number(parentIdChain.pop() ?? 0));
364
- const fullIdChain: string[] = [...parentIdChain.map(String), currentId];
365
+ const currentId = toAlpha(Number(parentIdChain.pop() ?? 0) as number);
366
+ const fullIdChain: string[] = [...parentIdChain.map(v => String(v)) as string[], currentId];
365
367
  const idStr = fullIdChain.join('-');
366
368
 
367
369
  if (hasNested) {
@@ -393,7 +395,7 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }, scop
393
395
 
394
396
  const measureFn = async <T = null>(
395
397
  arg1: string | object,
396
- arg2?: ((measure: MeasureFn) => Promise<T>) | ((measure: MeasureFn) => T),
398
+ arg2?: ((measure: MeasureFn, measureSync: MeasureSyncFn) => Promise<T>) | ((measure: MeasureFn) => T),
397
399
  arg3?: (error: unknown) => any
398
400
  ): Promise<T | null> => {
399
401
  if (typeof arg2 === 'function') {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "module": "index.ts",
4
4
  "main": "./index.ts",
5
5
  "types": "./index.ts",
6
- "version": "3.10.1",
6
+ "version": "3.11.0",
7
7
  "type": "module",
8
8
  "private": false,
9
9
  "description": "Zero-dependency function performance measurement with hierarchical logging",
@@ -30,4 +30,4 @@
30
30
  "test": "bun test",
31
31
  "example": "bun run example.ts"
32
32
  }
33
- }
33
+ }