fn-watch
v1.0.0
Published
Track and analyze function calls with timestamps, stack traces, and performance metrics.
Downloads
8
Maintainers
Readme
Foobar
Foobar is a Python library for dealing with word pluralization.
🔧 Installation
npm install function-tracker
Usage
function-tracker is a lightweight utility that wraps any function and gives you:
✅ Precise execution timing
✅ Automatic stack trace capture
✅ Call count tracking
✅ Optional debounce/throttle support
✅ Hook to log or process each call (onCall)
✅ Zero dependencies — works in browser & Node.js
feature
🧠 Wrap any function
🕒 Track call time & duration
🔁 Log how many times it's been called
📍 See where it was called from
⏳ Debounce or throttle execution
🧪 Inject analytics or custom behavior
Uses
import { trackFunction } from 'function-tracker';
function greet(name: string) {
console.log(`Hello, ${name}!`);
}
const trackedGreet = trackFunction(greet, {
name: 'greet',
log: true,
onCall({ args, callCount, duration }) {
console.log(`Custom onCall → args: ${args}, call #: ${callCount}, duration: ${duration.toFixed(2)}ms`);
}
});
trackedGreet('Alice');
trackedGreet('Bob');
.
📤 Output
[🧠 greet]
⏱️ Time: 2:43:10 PM
📍 Called from: at demo.ts:6:18
🔁 Call count: 2
✅ Execution time: 0.30ms
Custom onCall → args: Alice, call #: 2, duration: 0.30ms
⏳ Debounce & Throttle
🔁 Debounce
Wait X milliseconds after the last call before running.
ts
Copy
Edit
const debouncedFn = trackFunction(() => console.log("Fired!"), {
debounce: 300
});
debouncedFn(); // skipped
debouncedFn(); // skipped
setTimeout(debouncedFn, 500); // ✅ only this runs
🔁 Throttle
Only allow one call every X milliseconds.
ts
Copy
Edit
const throttledFn = trackFunction(() => console.log("Throttled!"), {
throttle: 1000
});
throttledFn(); // ✅ runs
throttledFn(); // ❌ ignored (too soon)
setTimeout(throttledFn, 1200); // ✅ runs again
🧪 Full API
trackFunction(fn, options): WrappedFn
Option Type Default Description
name string fn.name Optional name used in logs
debounce number (ms) 0 Delay execution until X ms after last call
throttle number (ms) 0 Allow only one call per X ms
log boolean true Enable or disable console logs
onCall function({ ...data }) () => {} Callback executed after every actual call
🔄 Cancel Pending Calls
You can cancel a debounced call if needed:
ts
Copy
Edit
const fn = trackFunction(() => console.log("cancel me"), { debounce: 300 });
fn();
fn.cancel?.(); // 🚫 cancel before it runs
📊 onCall Hook
Use onCall to track metrics or integrate with analytics:
ts
Copy
Edit
trackFunction(myFn, {
onCall: ({ args, result, duration, time, callCount, stack }) => {
// Log or send data
}
});
Data structure:
ts
Copy
Edit
{
args: any[]; // Arguments passed
result: any; // Return value
duration: number; // ms
time: number; // timestamp
stack: string; // where it was called from
callCount: number; // total calls
}
🌐 Compatibility
✅ Browser (modern + legacy)
✅ Node.js
✅ Supports both CommonJS and ESM
✅ Fully tree-shakable and zero dependencies
🔧 Example Use Cases
Debugging production or complex async code
Tracking how many times a callback was triggered
Creating internal profiling/logging tools
Visualizing performance bottlenecks
Teaching function execution behavior
🪪 License
MIT © Your Name
