measure
Signature
Section titled “Signature”async measure<T>(label: string, fn: () => Promise<T> | T): Promise<T>Description
Section titled “Description”The measure utility is a high-level wrapper around time() and timeEnd(). It handles the lifecycle of a performance timer automatically, ensuring the result is logged even if the provided function throws an error.
Type Parameters
Section titled “Type Parameters”| Parameter | Description |
|---|---|
<T> | The return type of the function being measured. Infers automatically. |
Examples
Section titled “Examples”Measuring an API Request
Section titled “Measuring an API Request”Perfect for identifying slow external dependencies.
const userData = await zylog.measure('fetch-user-api', async () => { const response = await fetch('https://api.example.com/user/1'); return response.json();});Profiling Database Queries
Section titled “Profiling Database Queries”Use it to monitor SQL execution time in development.
const users = await zylog.measure('db-query-all-users', () => { return db.select().from(usersTable);});Handling Nested Logic
Section titled “Handling Nested Logic”measure handles both synchronous and asynchronous functions seamlessly.
function heavyComputation() { // ... math logic return 42;}
const result = await zylog.measure('complex-math', heavyComputation);