measure-fn 3.10.2 → 3.11.1

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 (3) hide show
  1. package/index.ts +15 -10
  2. package/package.json +1 -1
  3. package/bun.lock +0 -26
package/index.ts CHANGED
@@ -165,6 +165,7 @@ const emit = (event: MeasureEvent, prefix?: string) => {
165
165
  defaultLogger(event, prefix);
166
166
  };
167
167
 
168
+
168
169
  const defaultLogger = (event: MeasureEvent, prefix?: string) => {
169
170
  const pfx = prefix ? `${prefix}:` : '';
170
171
  const id = `[${pfx}${event.id}]`;
@@ -175,7 +176,7 @@ const defaultLogger = (event: MeasureEvent, prefix?: string) => {
175
176
  console.log(`${t}${id} ... ${event.label}${formatMeta(event.meta)}`);
176
177
  break;
177
178
  case 'success': {
178
- const endLabel = dotEndLabel ? dotChar.repeat(event.label.length) : event.label;
179
+ const endLabel = dotEndLabel ? dotChar.repeat(event.label.length + 5) : ` ${event.label}`;
179
180
  const resultStr = event.result !== undefined ? safeStringify(event.result, event.maxResultLength) : '';
180
181
  const arrow = resultStr ? ` → ${resultStr}` : '';
181
182
  const budgetWarn = event.budget && event.duration! > event.budget
@@ -185,7 +186,8 @@ const defaultLogger = (event: MeasureEvent, prefix?: string) => {
185
186
  break;
186
187
  }
187
188
  case 'error': {
188
- const endLabel = dotEndLabel ? dotChar.repeat(event.label.length) : event.label;
189
+ // Added + 3 to the repeat count because ' ✗ ' takes up 3 characters, needing 5 total to match ' ... '
190
+ const endLabel = dotEndLabel ? dotChar.repeat(event.label.length + 3) : ` ${event.label}`;
189
191
  const errorMsg = event.error instanceof Error ? event.error.message : String(event.error);
190
192
  const budgetWarn = event.budget && event.duration! > event.budget
191
193
  ? ` ⚠ OVER BUDGET (${formatDuration(event.budget)})`
@@ -209,8 +211,10 @@ const defaultLogger = (event: MeasureEvent, prefix?: string) => {
209
211
 
210
212
  // ─── Types ───────────────────────────────────────────────────────────
211
213
 
214
+
212
215
  export type MeasureFn = {
213
216
  <U>(label: string | object, fn: () => Promise<U>): Promise<U | null>;
217
+ <U>(label: string | object, fn: (m: MeasureFn, ms: MeasureSyncFn) => Promise<U>): Promise<U | null>;
214
218
  <U>(label: string | object, fn: (m: MeasureFn) => Promise<U>): Promise<U | null>;
215
219
  (label: string | object): Promise<null>;
216
220
  };
@@ -283,7 +287,7 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }, scop
283
287
  let _lastError: unknown = null;
284
288
 
285
289
  const _measureInternal = async <U>(
286
- fnInternal: (measure: MeasureFn) => Promise<U>,
290
+ fnInternal: (measure: MeasureFn, measureSync: MeasureSyncFn) => Promise<U>,
287
291
  actionInternal: string | object,
288
292
  parentIdChain: (string | number)[],
289
293
  depth: number,
@@ -298,8 +302,8 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }, scop
298
302
  const localMaxLen = extractMaxResultLength(actionInternal);
299
303
  const effectiveMaxLen = localMaxLen ?? inheritedMaxLen;
300
304
 
301
- const currentId = toAlpha(Number(parentIdChain.pop() ?? 0));
302
- const fullIdChain: string[] = [...parentIdChain.map(v => String(v)), currentId];
305
+ const currentId = toAlpha(Number(parentIdChain.pop() ?? 0) as number);
306
+ const fullIdChain: string[] = [...parentIdChain.map(v => String(v)) as string[], currentId];
303
307
  const idStr = fullIdChain.join('-');
304
308
 
305
309
  emit({
@@ -311,18 +315,19 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }, scop
311
315
  }, prefix);
312
316
 
313
317
  const measureForNextLevel = createNestedResolver(true, fullIdChain, childCounterRef, depth, _measureInternal, prefix, effectiveMaxLen);
318
+ const measureSyncForNextLevel = createNestedResolver(false, fullIdChain, childCounterRef, depth, _measureInternalSync, prefix, effectiveMaxLen);
314
319
 
315
320
  try {
316
321
  let result: U;
317
322
  if (timeout && timeout > 0) {
318
323
  result = await Promise.race([
319
- fnInternal(measureForNextLevel as MeasureFn),
324
+ fnInternal(measureForNextLevel as MeasureFn, measureSyncForNextLevel as MeasureSyncFn),
320
325
  new Promise<never>((_, reject) =>
321
326
  setTimeout(() => reject(new Error(`Timeout (${formatDuration(timeout)})`)), timeout)
322
327
  ),
323
328
  ]);
324
329
  } else {
325
- result = await fnInternal(measureForNextLevel as MeasureFn);
330
+ result = await fnInternal(measureForNextLevel as MeasureFn, measureSyncForNextLevel as MeasureSyncFn);
326
331
  }
327
332
  const duration = performance.now() - start;
328
333
  emit({ type: 'success', id: idStr, label, depth, duration, result, budget, maxResultLength: effectiveMaxLen }, prefix);
@@ -360,8 +365,8 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }, scop
360
365
  const localMaxLen = extractMaxResultLength(actionInternal);
361
366
  const effectiveMaxLen = localMaxLen ?? inheritedMaxLen;
362
367
 
363
- const currentId = toAlpha(Number(parentIdChain.pop() ?? 0));
364
- const fullIdChain: string[] = [...parentIdChain.map(v => String(v)), currentId];
368
+ const currentId = toAlpha(Number(parentIdChain.pop() ?? 0) as number);
369
+ const fullIdChain: string[] = [...parentIdChain.map(v => String(v)) as string[], currentId];
365
370
  const idStr = fullIdChain.join('-');
366
371
 
367
372
  if (hasNested) {
@@ -393,7 +398,7 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }, scop
393
398
 
394
399
  const measureFn = async <T = null>(
395
400
  arg1: string | object,
396
- arg2?: ((measure: MeasureFn) => Promise<T>) | ((measure: MeasureFn) => T),
401
+ arg2?: ((measure: MeasureFn, measureSync: MeasureSyncFn) => Promise<T>) | ((measure: MeasureFn) => T),
397
402
  arg3?: (error: unknown) => any
398
403
  ): Promise<T | null> => {
399
404
  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.2",
6
+ "version": "3.11.1",
7
7
  "type": "module",
8
8
  "private": false,
9
9
  "description": "Zero-dependency function performance measurement with hierarchical logging",
package/bun.lock DELETED
@@ -1,26 +0,0 @@
1
- {
2
- "lockfileVersion": 1,
3
- "configVersion": 0,
4
- "workspaces": {
5
- "": {
6
- "name": "ments-utils",
7
- "devDependencies": {
8
- "@types/bun": "latest",
9
- },
10
- "peerDependencies": {
11
- "typescript": "^5",
12
- },
13
- },
14
- },
15
- "packages": {
16
- "@types/bun": ["@types/bun@1.2.17", "", { "dependencies": { "bun-types": "1.2.17" } }, "sha512-l/BYs/JYt+cXA/0+wUhulYJB6a6p//GTPiJ7nV+QHa8iiId4HZmnu/3J/SowP5g0rTiERY2kfGKXEK5Ehltx4Q=="],
17
-
18
- "@types/node": ["@types/node@24.0.4", "", { "dependencies": { "undici-types": "~7.8.0" } }, "sha512-ulyqAkrhnuNq9pB76DRBTkcS6YsmDALy6Ua63V8OhrOBgbcYt6IOdzpw5P1+dyRIyMerzLkeYWBeOXPpA9GMAA=="],
19
-
20
- "bun-types": ["bun-types@1.2.17", "", { "dependencies": { "@types/node": "*" } }, "sha512-ElC7ItwT3SCQwYZDYoAH+q6KT4Fxjl8DtZ6qDulUFBmXA8YB4xo+l54J9ZJN+k2pphfn9vk7kfubeSd5QfTVJQ=="],
21
-
22
- "typescript": ["typescript@5.8.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ=="],
23
-
24
- "undici-types": ["undici-types@7.8.0", "", {}, "sha512-9UJ2xGDvQ43tYyVMpuHlsgApydB8ZKfVYTsLDhXkFL/6gfkp+U8xTGdh8pMJv1SpZna0zxG1DwsKZsreLbXBxw=="],
25
- }
26
- }