Skip to content

measure

async measure<T>(label: string, fn: () => Promise<T> | T): Promise<T>

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.

ParameterDescription
<T>The return type of the function being measured. Infers automatically.

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();
});

Use it to monitor SQL execution time in development.

const users = await zylog.measure('db-query-all-users', () => {
return db.select().from(usersTable);
});

measure handles both synchronous and asynchronous functions seamlessly.

function heavyComputation() {
// ... math logic
return 42;
}
const result = await zylog.measure('complex-math', heavyComputation);