Skip to main content

Performance Logging

Logluxe makes performance measurement dead simple with built-in timing utilities.

Basic Timing

log.perf(label)

Start a performance timer:

import { log } from 'logluxe';

// Start timer
log.perf('database-query');

// ... do some work ...
await fetchDataFromDatabase();

// End timer and print duration
log.perfEnd('database-query');
Output
database-query: 145.23ms
🎮 Try Performance Logging

Automatic Formatting

Duration is automatically formatted for readability:

// Milliseconds
log.perfEnd('quick-task');
// Seconds
log.perfEnd('medium-task');
// Minutes
log.perfEnd('long-task');
Duration Formats
quick-task: 45.23ms
medium-task: 2.45s
long-task: 1m 30.5s

Nested Timers

Track multiple operations simultaneously:

log.perf('total');

log.perf('fetch');
const data = await fetchData();
log.perfEnd('fetch');

log.perf('process');
const processed = processData(data);
log.perfEnd('process');

log.perf('save');
await saveData(processed);
log.perfEnd('save');

log.perfEnd('total');
Nested Timers
fetch: 234.56ms
process: 12.34ms
save: 89.01ms
total: 335.91ms
🎮 Try Nested Timers

Return Duration Value

log.perfEnd(label) returns duration

log.perf('operation');
await doSomething();
const duration = log.perfEnd('operation');

console.log(`Operation took ${duration}ms`);

if (duration > 1000) {
log.warn('Operation took longer than expected');
}
Output
operation: 1234.56ms
Operation took 1234.56ms
Operation took longer than expected

Silent Timing

Get duration without logging

log.perf('silent-op');
await doWork();

// Get duration without printing
const duration = log.perfValue('silent-op');

// Use duration in your own message
log.info(`Custom message: ${duration}ms`);
Output
Custom message: 156.78ms

Practical Examples

API Request Timing

async function fetchUser(id) {
log.perf(`fetch-user-${id}`);

try {
const response = await fetch(`/api/users/${id}`);
const user = await response.json();

const duration = log.perfEnd(`fetch-user-${id}`);

if (duration > 500) {
log.warn(`Slow API response: ${duration}ms`);
}

return user;
} catch (error) {
log.perfEnd(`fetch-user-${id}`);
throw error;
}
}
API Response Time
fetch-user-123: 145.23ms
fetch-user-456: 523.45ms
Slow API response: 523.45ms
🎮 Try API Timing

Database Query Profiling

class DatabaseProfiler {
async query(sql, params) {
const label = `query-${Date.now()}`;
log.perf(label);

log.debug(`Executing: ${sql.slice(0, 50)}...`);

try {
const result = await this.db.query(sql, params);
const duration = log.perfEnd(label);

// Log slow queries
if (duration > 100) {
log.warn(`Slow query detected (${duration}ms): ${sql}`);
}

return result;
} catch (error) {
log.perfEnd(label);
throw error;
}
}
}
Query Profiling
Executing: SELECT * FROM users WHERE id = $1...
query-1705312800000: 23.45ms
Executing: SELECT * FROM orders WHERE user_id = ...
query-1705312800100: 156.78ms
Slow query detected (156.78ms): SELECT * FROM orders WHERE user_id = $1
🎮 Try Query Profiling

Build Step Timing

async function build() {
log.info('Starting build...');
log.perf('build');

log.perf('typescript');
await compileTypeScript();
log.perfEnd('typescript');

log.perf('webpack');
await runWebpack();
log.perfEnd('webpack');

log.perf('optimize');
await optimizeAssets();
log.perfEnd('optimize');

const total = log.perfEnd('build');
log.success(`Build complete in ${total}ms`);
}
Build Timing
Starting build...
typescript: 1.23s
webpack: 3.45s
optimize: 0.89s
build: 5.57s
Build complete in 5570ms
🎮 Try Build Timing

Function Wrapper

// Create a timing wrapper for any function
function withTiming(label, fn) {
return async (...args) => {
log.perf(label);
try {
const result = await fn(...args);
log.perfEnd(label);
return result;
} catch (error) {
log.perfEnd(label);
throw error;
}
};
}

// Usage
const timedFetch = withTiming('fetch-data', fetchData);
const result = await timedFetch(url);
Output
fetch-data: 234.56ms

Performance Summary

class PerformanceTracker {
constructor() {
this.metrics = {};
}

track(label) {
log.perf(label);
return () => {
const duration = log.perfEnd(label);
this.metrics[label] = duration;
};
}

printSummary() {
log.info('Performance Summary:');
log.divider();

Object.entries(this.metrics)
.sort((a, b) => b[1] - a[1])
.forEach(([label, duration]) => {
const color = duration > 1000 ? 'red' : duration > 100 ? 'yellow' : 'green';
log.tagged(` ${label}: [${color}]${duration}ms[/${color}]`);
});

log.divider();
}
}
Performance Summary
Performance Summary:
────────────────────────────────────────
webpack: 3450ms
typescript: 1230ms
optimize: 89ms
cleanup: 12ms
────────────────────────────────────────
🎮 Try Summary

Next: Log Grouping