typed-time-log
v1.0.0
Published
A small utility for logging the execution time of functions making console.time type-safe.
Downloads
3
Maintainers
Readme
Typed Console Time
A lightweight, modern, and type-safe utility for timing operations in TypeScript and JavaScript. This package provides a cleaner and more robust alternative to the standard console.time and console.timeEnd, especially when dealing with asynchronous code and modern resource management patterns.
Key Features
- Type-Safe: Written in TypeScript for a great developer experience.
- Flexible: Works with both synchronous and asynchronous functions.
- Modern: Supports the experimental
usingkeyword for automatic, error-proof cleanup. - Zero Dependencies: A tiny, focused utility that adds minimal overhead.
Installation
You can use your favorite package manager to install it:
npm install typed-console-timeUsage
There are three primary ways to use this utility, depending on your needs.
1. Manual Start and End
If you need full control over the timer's lifecycle, you can manage it manually. This is the most universally compatible approach.
import { startTimeLog } from "typed-console-time";
// A simple sleep function to simulate async work
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
async function fetchUserData() {
const timer = startTimeLog("Fetch user data"); // Timer starts
await sleep(500); // Simulate a network request
timer.end(); // Manually stop the timer
}
fetchUserData();
// Expected output:
// Fetch user data: 501.456ms2. Wrapping a Function Call
For timing a single function (synchronous or asynchronous), withTimeLog is a convenient wrapper that handles starting and stopping the timer for you. It returns the result of the wrapped function.
import { withTimeLog } from "typed-console-time";
// A synchronous function that takes some time
const heavySyncCalculation = () => {
let sum = 0;
for (let i = 0; i < 1e8; i++) {
sum += i;
}
return sum;
};
// An async function that simulates a delay
const fetchFromApi = async () => {
await new Promise((resolve) => setTimeout(resolve, 300));
return { id: 1, name: "John Doe" };
};
async function main() {
// Time a synchronous function
const result = await withTimeLog("Sync calculation", () =>
heavySyncCalculation()
);
console.log(`Calculation result: ${result}`);
// Time an asynchronous function
const user = await withTimeLog("API fetch", () => fetchFromApi());
console.log(`Fetched user: ${user.name}`);
}
main();
// Expected output:
// Sync calculation: 150.789ms
// Calculation result: 4999999950000000
// API fetch: 302.111ms
// Fetched user: John Doe3. Automatic Cleanup with using (Experimental)
This is the cleanest way to time a block of code, leveraging the new Explicit Resource Management feature. The timer is automatically stopped when the block is exited, even if an error occurs.
Warning: The
usingkeyword is an experimental feature. As of late 2025, it is not supported in Safari and Node.js. It requires setting your TypeScripttargettoES2022or newer.You can learn more about its status and usage on MDN Web Docs.
import { startTimeLog } from "typed-console-time";
function processData() {
// The timer starts here
using _timer = startTimeLog("Data processing operation");
// Simulate some time-consuming work
for (let i = 0; i < 1e7; i++) {
// ... crunching numbers ...
}
console.log("Finished processing.");
} // The timer automatically ends here and logs the duration
processData();
// Expected output:
// Finished processing.
// Data processing operation: 25.123ms