Skip to main content

Overview

The Profiler class provides utilities for measuring execution time and performance profiling.

Import

import { Profiler, withTiming } from "bytekit";
// or
import { Profiler } from "bytekit/profiler";

Profiler Class

start

Start a new timer.
const profiler = new Profiler();
profiler.start("operation-name");

end

End a timer and return elapsed time.
const elapsed = profiler.end("operation-name");
console.log(`Operation took ${elapsed}ms`);

measure

Measure a synchronous operation.
const result = profiler.measure("operation", () => {
  // Your code here
  return computeResult();
});

measureAsync

Measure an async operation.
const result = await profiler.measureAsync("fetch-users", async () => {
  return await api.get("/users");
});

withTiming Helper

Wrap a function to automatically log execution time.
import { withTiming } from "bytekit";

const fetchUsers = withTiming(async () => {
  return await api.get("/users");
}, "fetch-users");

const users = await fetchUsers();
// Logs: "fetch-users completed in 123ms"

Usage Example

import { Profiler, createLogger } from "bytekit";

const profiler = new Profiler();
const logger = createLogger({ namespace: "perf" });

// Manual timing
profiler.start("db-query");
const users = await db.users.findMany();
const elapsed = profiler.end("db-query");
logger.info(`Query took ${elapsed}ms`);

// Automatic timing
const result = await profiler.measureAsync("api-call", async () => {
  return await api.get("/data");
});

Integration with ApiClient

import { ApiClient, createLogger, Profiler } from "bytekit";

const profiler = new Profiler();
const logger = createLogger({ namespace: "api" });

const api = new ApiClient({
  baseUrl: "https://api.example.com",
  logger,
  interceptors: {
    request: (url, init) => {
      profiler.start(url);
      return [url, init];
    },
    response: (response) => {
      const elapsed = profiler.end(response.url);
      logger.info(`Request completed in ${elapsed}ms`, { url: response.url });
      return response;
    }
  }
});

See Also