perf-monitor-pro
v1.0.0
Published
A stylish performance monitor for catching slow functions in Node.js
Maintainers
Readme
Have you ever wondered why your API request takes 2 seconds to respond, but you don't know which function is causing the bottleneck?
perf-monitor-pro makes it incredibly easy to find slow functions. Just wrap a function or use the @monitor decorator. If it takes longer than your defined threshold, you'll see a beautiful warning in your terminal!
✨ Features
- 🚀 Two Easy Ways: Use decorators for Object-Oriented code (
@monitor), or wrapper functions (withMonitor) for functional code. - 🎨 Beautiful Developer Experience (DX): Colored, icon-supported logs only when a function exceeds the defined speed limit.
- ⏱️ Zero Overhead on Fast Functions: If your function is faster than the threshold, it stays completely silent. No terminal noise!
- 🤝 Works with Everything: Fully supports
async/await, Promises, and synchronous functions automatically.
🎨 Terminal Experience
When a function takes longer than the threshold (e.g., 200ms), you get a clear warning. If it takes twice as long, it turns critical (red)!
⚠️ [perf-monitor-pro] Slow function detected: [Method] fetchUsers took 301.57ms (Threshold: 200ms)
🔥 [perf-monitor-pro] Slow function detected: criticalQuery took 501.09ms (Threshold: 100ms)📦 Installation
npm install perf-monitor-pro picocolors
# or
yarn add perf-monitor-pro picocolors
# or
pnpm add perf-monitor-pro picocolorsNote for Decorator usage: If you want to use the
@monitordecorator, ensure"experimentalDecorators": trueis set in yourtsconfig.json.
🚀 Usage Guide
Method 1: Using the @monitor Decorator (For Classes)
If you are using Classes, simply drop the @monitor decorator above any method.
import { monitor } from 'perf-monitor-pro';
class DatabaseService {
// Warn if this method takes more than 200ms
@monitor({ warnThreshold: 200 })
async fetchUsers() {
// some heavy operation...
await db.query('SELECT * FROM users');
return users;
}
}Method 2: Using the withMonitor Wrapper (For Standalone Functions)
For regular functions or arrow functions, just wrap them with withMonitor.
import { withMonitor } from 'perf-monitor-pro';
// Wrap your function and set the threshold as the second argument
const processPayment = withMonitor(async (amount: number) => {
// some heavy operation...
await stripe.charge(amount);
return true;
}, { warnThreshold: 150 }); // Warn if exceeds 150ms
await processPayment(100);⚙️ Options
{
warnThreshold?: number; // The limit in milliseconds. Default is 200.
}📄 License
MIT © Ahmet Ozcan
