@fuzdev/fuz_util 0.42.0 → 0.44.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.
- package/LICENSE +1 -1
- package/README.md +19 -12
- package/dist/async.d.ts +2 -2
- package/dist/async.d.ts.map +1 -1
- package/dist/async.js +2 -2
- package/dist/benchmark.d.ts +179 -0
- package/dist/benchmark.d.ts.map +1 -0
- package/dist/benchmark.js +400 -0
- package/dist/benchmark_baseline.d.ts +195 -0
- package/dist/benchmark_baseline.d.ts.map +1 -0
- package/dist/benchmark_baseline.js +388 -0
- package/dist/benchmark_format.d.ts +87 -0
- package/dist/benchmark_format.d.ts.map +1 -0
- package/dist/benchmark_format.js +266 -0
- package/dist/benchmark_stats.d.ts +112 -0
- package/dist/benchmark_stats.d.ts.map +1 -0
- package/dist/benchmark_stats.js +219 -0
- package/dist/benchmark_types.d.ts +174 -0
- package/dist/benchmark_types.d.ts.map +1 -0
- package/dist/benchmark_types.js +1 -0
- package/dist/git.d.ts +12 -0
- package/dist/git.d.ts.map +1 -1
- package/dist/git.js +14 -0
- package/dist/library_json.d.ts +3 -3
- package/dist/library_json.d.ts.map +1 -1
- package/dist/library_json.js +1 -1
- package/dist/maths.d.ts +4 -0
- package/dist/maths.d.ts.map +1 -1
- package/dist/maths.js +8 -0
- package/dist/object.js +1 -1
- package/dist/source_json.d.ts +4 -4
- package/dist/stats.d.ts +180 -0
- package/dist/stats.d.ts.map +1 -0
- package/dist/stats.js +402 -0
- package/dist/string.d.ts +13 -0
- package/dist/string.d.ts.map +1 -1
- package/dist/string.js +58 -0
- package/dist/time.d.ts +165 -0
- package/dist/time.d.ts.map +1 -0
- package/dist/time.js +264 -0
- package/dist/timings.d.ts +1 -7
- package/dist/timings.d.ts.map +1 -1
- package/dist/timings.js +16 -16
- package/package.json +21 -19
- package/src/lib/async.ts +3 -3
- package/src/lib/benchmark.ts +498 -0
- package/src/lib/benchmark_baseline.ts +538 -0
- package/src/lib/benchmark_format.ts +314 -0
- package/src/lib/benchmark_stats.ts +311 -0
- package/src/lib/benchmark_types.ts +197 -0
- package/src/lib/git.ts +24 -0
- package/src/lib/library_json.ts +3 -3
- package/src/lib/maths.ts +8 -0
- package/src/lib/object.ts +1 -1
- package/src/lib/stats.ts +534 -0
- package/src/lib/string.ts +66 -0
- package/src/lib/time.ts +319 -0
- package/src/lib/timings.ts +17 -17
- package/src/lib/types.ts +2 -2
package/src/lib/time.ts
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Time utilities.
|
|
3
|
+
* Provides cross-platform high-resolution timing and measurement helpers.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Timer interface for measuring elapsed time.
|
|
8
|
+
* Returns time in nanoseconds for maximum precision.
|
|
9
|
+
*/
|
|
10
|
+
export interface Timer {
|
|
11
|
+
/** Get current time in nanoseconds */
|
|
12
|
+
now: () => number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Node.js high-resolution timer using process.hrtime.bigint().
|
|
17
|
+
* Provides true nanosecond precision.
|
|
18
|
+
*/
|
|
19
|
+
export const timer_node: Timer = {
|
|
20
|
+
now: (): number => {
|
|
21
|
+
const ns = process.hrtime.bigint();
|
|
22
|
+
return Number(ns); // Native nanoseconds
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Browser high-resolution timer using performance.now().
|
|
28
|
+
* Converts milliseconds to nanoseconds for consistent API.
|
|
29
|
+
*
|
|
30
|
+
* **Precision varies by browser due to Spectre/Meltdown mitigations:**
|
|
31
|
+
* - Chrome: ~100μs (coarsened)
|
|
32
|
+
* - Firefox: ~1ms (rounded)
|
|
33
|
+
* - Safari: ~100μs
|
|
34
|
+
* - Node.js: ~1μs
|
|
35
|
+
*
|
|
36
|
+
* For nanosecond-precision benchmarks, use Node.js with `timer_node`.
|
|
37
|
+
*/
|
|
38
|
+
export const timer_browser: Timer = {
|
|
39
|
+
now: (): number => {
|
|
40
|
+
return performance.now() * 1_000_000; // Convert ms to ns
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Detect the best available timer function for the current environment.
|
|
46
|
+
* Called once and cached for performance.
|
|
47
|
+
*/
|
|
48
|
+
const detect_timer_fn = (): (() => number) => {
|
|
49
|
+
// Check if we're in Node.js with hrtime.bigint support
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
51
|
+
if (typeof process !== 'undefined' && process.hrtime) {
|
|
52
|
+
try {
|
|
53
|
+
if (typeof process.hrtime.bigint !== 'undefined') {
|
|
54
|
+
return timer_node.now;
|
|
55
|
+
}
|
|
56
|
+
} catch {
|
|
57
|
+
// Ignore and fall through
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// Fallback to performance.now() (works in browsers and modern Node.js)
|
|
61
|
+
if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
|
|
62
|
+
return timer_browser.now;
|
|
63
|
+
}
|
|
64
|
+
// Last resort: Date.now() (millisecond precision only)
|
|
65
|
+
return () => Date.now() * 1_000_000;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// Cache the detected timer function
|
|
69
|
+
let _cached_timer_fn: (() => number) | null = null;
|
|
70
|
+
const get_timer_fn = (): (() => number) => {
|
|
71
|
+
if (_cached_timer_fn === null) {
|
|
72
|
+
_cached_timer_fn = detect_timer_fn();
|
|
73
|
+
}
|
|
74
|
+
return _cached_timer_fn;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Auto-detected timer based on environment.
|
|
79
|
+
* Uses process.hrtime in Node.js, performance.now() in browsers.
|
|
80
|
+
* The timer function is detected once and cached for performance.
|
|
81
|
+
*/
|
|
82
|
+
export const timer_default: Timer = {
|
|
83
|
+
now: (): number => get_timer_fn()(),
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Time units and conversions.
|
|
88
|
+
*/
|
|
89
|
+
export const TIME_NS_PER_US = 1_000;
|
|
90
|
+
export const TIME_NS_PER_MS = 1_000_000;
|
|
91
|
+
export const TIME_NS_PER_SEC = 1_000_000_000;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Convert nanoseconds to microseconds.
|
|
95
|
+
*/
|
|
96
|
+
export const time_ns_to_us = (ns: number): number => ns / TIME_NS_PER_US;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Convert nanoseconds to milliseconds.
|
|
100
|
+
*/
|
|
101
|
+
export const time_ns_to_ms = (ns: number): number => ns / TIME_NS_PER_MS;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Convert nanoseconds to seconds.
|
|
105
|
+
*/
|
|
106
|
+
export const time_ns_to_sec = (ns: number): number => ns / TIME_NS_PER_SEC;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Time unit for formatting.
|
|
110
|
+
*/
|
|
111
|
+
export type TimeUnit = 'ns' | 'us' | 'ms' | 's';
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Display labels for time units (uses proper Unicode μ for microseconds).
|
|
115
|
+
*/
|
|
116
|
+
export const TIME_UNIT_DISPLAY: Record<TimeUnit, string> = {ns: 'ns', us: 'μs', ms: 'ms', s: 's'};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Detect the best time unit for a set of nanosecond values.
|
|
120
|
+
* Chooses the unit where most values fall in the range 1-9999.
|
|
121
|
+
* @param values_ns - Array of times in nanoseconds
|
|
122
|
+
* @returns Best unit to use for all values
|
|
123
|
+
*/
|
|
124
|
+
export const time_unit_detect_best = (values_ns: Array<number>): TimeUnit => {
|
|
125
|
+
if (values_ns.length === 0) return 'ms';
|
|
126
|
+
|
|
127
|
+
// Filter out invalid values
|
|
128
|
+
const valid = values_ns.filter((v) => isFinite(v) && v > 0);
|
|
129
|
+
if (valid.length === 0) return 'ms';
|
|
130
|
+
|
|
131
|
+
// Find the median value (more stable than mean for outliers)
|
|
132
|
+
const sorted = [...valid].sort((a, b) => a - b);
|
|
133
|
+
const median = sorted[Math.floor(sorted.length / 2)]!;
|
|
134
|
+
|
|
135
|
+
// Choose unit based on median magnitude
|
|
136
|
+
if (median < 1_000) {
|
|
137
|
+
return 'ns'; // < 1μs
|
|
138
|
+
} else if (median < 1_000_000) {
|
|
139
|
+
return 'us'; // < 1ms
|
|
140
|
+
} else if (median < 1_000_000_000) {
|
|
141
|
+
return 'ms'; // < 1s
|
|
142
|
+
} else {
|
|
143
|
+
return 's'; // >= 1s
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Format time with a specific unit.
|
|
149
|
+
* @param ns - Time in nanoseconds
|
|
150
|
+
* @param unit - Unit to use ('ns', 'us', 'ms', 's')
|
|
151
|
+
* @param decimals - Number of decimal places (default: 2)
|
|
152
|
+
* @returns Formatted string like "3.87μs"
|
|
153
|
+
*/
|
|
154
|
+
export const time_format = (ns: number, unit: TimeUnit, decimals: number = 2): string => {
|
|
155
|
+
if (!isFinite(ns)) return String(ns);
|
|
156
|
+
|
|
157
|
+
switch (unit) {
|
|
158
|
+
case 'ns':
|
|
159
|
+
return `${ns.toFixed(decimals)}ns`;
|
|
160
|
+
case 'us':
|
|
161
|
+
return `${time_ns_to_us(ns).toFixed(decimals)}μs`;
|
|
162
|
+
case 'ms':
|
|
163
|
+
return `${time_ns_to_ms(ns).toFixed(decimals)}ms`;
|
|
164
|
+
case 's':
|
|
165
|
+
return `${time_ns_to_sec(ns).toFixed(decimals)}s`;
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Format time with adaptive units (ns/μs/ms/s) based on magnitude.
|
|
171
|
+
* @param ns - Time in nanoseconds
|
|
172
|
+
* @param decimals - Number of decimal places (default: 2)
|
|
173
|
+
* @returns Formatted string like "3.87μs" or "1.23ms"
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* time_format_adaptive(1500) // "1.50μs"
|
|
178
|
+
* time_format_adaptive(3870) // "3.87μs"
|
|
179
|
+
* time_format_adaptive(1500000) // "1.50ms"
|
|
180
|
+
* time_format_adaptive(1500000000) // "1.50s"
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
export const time_format_adaptive = (ns: number, decimals: number = 2): string => {
|
|
184
|
+
if (!isFinite(ns)) return String(ns);
|
|
185
|
+
|
|
186
|
+
// Choose unit based on magnitude
|
|
187
|
+
if (ns < 1_000) {
|
|
188
|
+
return time_format(ns, 'ns', decimals);
|
|
189
|
+
} else if (ns < 1_000_000) {
|
|
190
|
+
return time_format(ns, 'us', decimals);
|
|
191
|
+
} else if (ns < 1_000_000_000) {
|
|
192
|
+
return time_format(ns, 'ms', decimals);
|
|
193
|
+
} else {
|
|
194
|
+
return time_format(ns, 's', decimals);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Result from timing a function execution.
|
|
200
|
+
* All times in nanoseconds for maximum precision.
|
|
201
|
+
*/
|
|
202
|
+
export interface TimeResult {
|
|
203
|
+
/** Elapsed time in nanoseconds */
|
|
204
|
+
elapsed_ns: number;
|
|
205
|
+
/** Elapsed time in microseconds (convenience) */
|
|
206
|
+
elapsed_us: number;
|
|
207
|
+
/** Elapsed time in milliseconds (convenience) */
|
|
208
|
+
elapsed_ms: number;
|
|
209
|
+
/** Start time in nanoseconds (from timer.now()) */
|
|
210
|
+
started_at_ns: number;
|
|
211
|
+
/** End time in nanoseconds (from timer.now()) */
|
|
212
|
+
ended_at_ns: number;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Time an asynchronous function execution.
|
|
217
|
+
* @param fn - Async function to time
|
|
218
|
+
* @param timer - Timer to use (defaults to timer_default)
|
|
219
|
+
* @returns Object containing the function result and timing information
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```ts
|
|
223
|
+
* const {result, timing} = await time_async(async () => {
|
|
224
|
+
* await fetch('https://api.example.com/data');
|
|
225
|
+
* return 42;
|
|
226
|
+
* });
|
|
227
|
+
* console.log(`Result: ${result}, took ${time_format_adaptive(timing.elapsed_ns)}`);
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
export const time_async = async <T>(
|
|
231
|
+
fn: () => Promise<T>,
|
|
232
|
+
timer: Timer = timer_default,
|
|
233
|
+
): Promise<{result: T; timing: TimeResult}> => {
|
|
234
|
+
const started_at_ns = timer.now();
|
|
235
|
+
const result = await fn();
|
|
236
|
+
const ended_at_ns = timer.now();
|
|
237
|
+
const elapsed_ns = ended_at_ns - started_at_ns;
|
|
238
|
+
|
|
239
|
+
return {
|
|
240
|
+
result,
|
|
241
|
+
timing: {
|
|
242
|
+
elapsed_ns,
|
|
243
|
+
elapsed_us: time_ns_to_us(elapsed_ns),
|
|
244
|
+
elapsed_ms: time_ns_to_ms(elapsed_ns),
|
|
245
|
+
started_at_ns,
|
|
246
|
+
ended_at_ns,
|
|
247
|
+
},
|
|
248
|
+
};
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Time a synchronous function execution.
|
|
253
|
+
* @param fn - Sync function to time
|
|
254
|
+
* @param timer - Timer to use (defaults to timer_default)
|
|
255
|
+
* @returns Object containing the function result and timing information
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```ts
|
|
259
|
+
* const {result, timing} = time_sync(() => {
|
|
260
|
+
* return expensive_computation();
|
|
261
|
+
* });
|
|
262
|
+
* console.log(`Result: ${result}, took ${time_format_adaptive(timing.elapsed_ns)}`);
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
export const time_sync = <T>(
|
|
266
|
+
fn: () => T,
|
|
267
|
+
timer: Timer = timer_default,
|
|
268
|
+
): {result: T; timing: TimeResult} => {
|
|
269
|
+
const started_at_ns = timer.now();
|
|
270
|
+
const result = fn();
|
|
271
|
+
const ended_at_ns = timer.now();
|
|
272
|
+
const elapsed_ns = ended_at_ns - started_at_ns;
|
|
273
|
+
|
|
274
|
+
return {
|
|
275
|
+
result,
|
|
276
|
+
timing: {
|
|
277
|
+
elapsed_ns,
|
|
278
|
+
elapsed_us: time_ns_to_us(elapsed_ns),
|
|
279
|
+
elapsed_ms: time_ns_to_ms(elapsed_ns),
|
|
280
|
+
started_at_ns,
|
|
281
|
+
ended_at_ns,
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Measure multiple executions of a function and return all timings.
|
|
288
|
+
* @param fn - Function to measure (sync or async)
|
|
289
|
+
* @param iterations - Number of times to execute
|
|
290
|
+
* @param timer - Timer to use (defaults to timer_default)
|
|
291
|
+
* @returns Array of elapsed times in nanoseconds
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```ts
|
|
295
|
+
* const timings_ns = await time_measure(async () => {
|
|
296
|
+
* await process_data();
|
|
297
|
+
* }, 100);
|
|
298
|
+
*
|
|
299
|
+
* import {BenchmarkStats} from './benchmark_stats.js';
|
|
300
|
+
* const stats = new BenchmarkStats(timings_ns);
|
|
301
|
+
* console.log(`Mean: ${time_format_adaptive(stats.mean_ns)}`);
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
export const time_measure = async (
|
|
305
|
+
fn: () => unknown,
|
|
306
|
+
iterations: number,
|
|
307
|
+
timer: Timer = timer_default,
|
|
308
|
+
): Promise<Array<number>> => {
|
|
309
|
+
const timings: Array<number> = [];
|
|
310
|
+
|
|
311
|
+
for (let i = 0; i < iterations; i++) {
|
|
312
|
+
const started_at_ns = timer.now();
|
|
313
|
+
await fn(); // eslint-disable-line no-await-in-loop
|
|
314
|
+
const ended_at_ns = timer.now();
|
|
315
|
+
timings.push(ended_at_ns - started_at_ns);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return timings;
|
|
319
|
+
};
|
package/src/lib/timings.ts
CHANGED
|
@@ -24,8 +24,8 @@ export type TimingsKey = string | number;
|
|
|
24
24
|
export class Timings {
|
|
25
25
|
readonly decimals: number | undefined;
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
readonly #timings: Map<TimingsKey, number | undefined> = new Map();
|
|
28
|
+
readonly #stopwatches: Map<TimingsKey, Stopwatch> = new Map();
|
|
29
29
|
|
|
30
30
|
constructor(decimals?: number) {
|
|
31
31
|
this.decimals = decimals;
|
|
@@ -35,41 +35,41 @@ export class Timings {
|
|
|
35
35
|
* Starts a timing operation for the given key.
|
|
36
36
|
*/
|
|
37
37
|
start(key: TimingsKey, decimals = this.decimals): () => number {
|
|
38
|
-
const final_key = this
|
|
39
|
-
this
|
|
40
|
-
this
|
|
41
|
-
return () => this
|
|
38
|
+
const final_key = this.#next_key(key);
|
|
39
|
+
this.#stopwatches.set(final_key, create_stopwatch(decimals));
|
|
40
|
+
this.#timings.set(final_key, undefined); // initializing to preserve order
|
|
41
|
+
return () => this.#stop(final_key);
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
if (!this
|
|
44
|
+
#next_key(key: TimingsKey): TimingsKey {
|
|
45
|
+
if (!this.#stopwatches.has(key)) return key;
|
|
46
46
|
let i = 2;
|
|
47
47
|
while (true) {
|
|
48
48
|
const next = key + '_' + i++;
|
|
49
|
-
if (!this
|
|
49
|
+
if (!this.#stopwatches.has(next)) return next;
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
/**
|
|
54
54
|
* Stops a timing operation and records the elapsed time.
|
|
55
55
|
*/
|
|
56
|
-
|
|
57
|
-
const stopwatch = this
|
|
56
|
+
#stop(key: TimingsKey): number {
|
|
57
|
+
const stopwatch = this.#stopwatches.get(key);
|
|
58
58
|
if (!stopwatch) return 0; // TODO maybe warn?
|
|
59
|
-
this
|
|
59
|
+
this.#stopwatches.delete(key);
|
|
60
60
|
const timing = stopwatch();
|
|
61
|
-
this
|
|
61
|
+
this.#timings.set(key, timing);
|
|
62
62
|
return timing;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
get(key: TimingsKey): number {
|
|
66
|
-
const timing = this
|
|
66
|
+
const timing = this.#timings.get(key);
|
|
67
67
|
if (timing === undefined) return 0; // TODO maybe warn?
|
|
68
68
|
return timing;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
entries(): IterableIterator<[TimingsKey, number | undefined]> {
|
|
72
|
-
return this
|
|
72
|
+
return this.#timings.entries();
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
/**
|
|
@@ -78,9 +78,9 @@ export class Timings {
|
|
|
78
78
|
*/
|
|
79
79
|
merge(timings: Timings): void {
|
|
80
80
|
for (const [key, timing] of timings.entries()) {
|
|
81
|
-
this
|
|
81
|
+
this.#timings.set(
|
|
82
82
|
key,
|
|
83
|
-
timing === undefined ? undefined : (this
|
|
83
|
+
timing === undefined ? undefined : (this.#timings.get(key) ?? 0) + timing,
|
|
84
84
|
);
|
|
85
85
|
}
|
|
86
86
|
}
|
package/src/lib/types.ts
CHANGED
|
@@ -21,9 +21,9 @@ export type OmitStrict<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
|
|
|
21
21
|
* @see https://stackoverflow.com/users/5770132/oblosys
|
|
22
22
|
*/
|
|
23
23
|
export type PickUnion<T, K extends KeyofUnion<T>> = T extends unknown
|
|
24
|
-
? K & keyof T extends never
|
|
24
|
+
? K & keyof T extends never
|
|
25
25
|
? never
|
|
26
|
-
: Pick<T, K & keyof T>
|
|
26
|
+
: Pick<T, K & keyof T>
|
|
27
27
|
: never;
|
|
28
28
|
|
|
29
29
|
/**
|