TracePerf

High-performance function execution tracking and monitoring for Node.js

Features

⚡️ High-Performance Tracking

Track function execution time with minimal overhead, even in production environments.

🧠 Memory Usage Monitoring

Track memory consumption of functions and identify potential memory leaks.

🔍 Flexible Tracking Modes

Choose between performance, balanced, and detailed tracking based on your needs.

📊 Execution Flow Visualization

Visualize execution flows with intelligent formatting to identify bottlenecks.

📦 Nested Function Tracking

Track function calls within other functions to understand complex execution flows.

⏱️ Configurable Thresholds

Focus on functions that exceed specific execution time thresholds.

🔄 Sampling Control

Adjust sampling rates to minimize performance impact in production.

🌐 Browser Support

Use the same powerful tracking in browser environments.

Getting Started

Installation

npm install traceperf

Basic Usage

const { createTracePerf, TrackingMode } = require('traceperf');

// Create a tracker with default settings
const traceperf = createTracePerf();

// Track a synchronous function
const result = traceperf.track(() => {
  // Your function logic here
  return 'result';
}, { label: 'myFunction' });

// Track an asynchronous function
async function main() {
  const result = await traceperf.track(async () => {
    // Your async function logic here
    return 'async result';
  }, { label: 'asyncFunction' });
}

main();

Tracking Modes

const { createTracePerf, TrackingMode } = require('traceperf');

// Performance mode - minimal overhead for production
const performanceTracker = createTracePerf({
  trackingMode: TrackingMode.PERFORMANCE,
  silent: true,
  threshold: 100, // only track functions that take more than 100ms
  sampleRate: 0.1 // only track 10% of function calls
});

// Detailed mode - maximum information for development
const devTracker = createTracePerf({
  trackingMode: TrackingMode.DETAILED,
  trackMemory: true,
  enableNestedTracking: true
});

Memory Tracking

const { createTracePerf } = require('traceperf');

// Enable memory tracking
const tracker = createTracePerf({ trackMemory: true });

// Track memory usage for a memory-intensive operation
const result = tracker.track(() => {
  // Memory intensive operations
  const largeArray = new Array(1000000).fill(Math.random());
  return processArray(largeArray);
}, { label: 'memoryIntensiveOperation' });

Output:

└─ memoryIntensiveOperation: 25.22ms 📊 11.29MB

Nested Function Tracking

const { createTracePerf } = require('traceperf');
const traceperf = createTracePerf({ enableNestedTracking: true });

// Define some functions
function processData(data) {
  return transformData(data);
}

function transformData(data) {
  return data.map(x => x * 2);
}

// Create trackable versions
const trackedProcessData = traceperf.createTrackable(processData, { 
  label: 'processData' 
});

const trackedTransformData = traceperf.createTrackable(transformData, { 
  label: 'transformData' 
});

// Execute with tracking
traceperf.track(() => {
  const data = [1, 2, 3, 4, 5];
  return trackedProcessData(data);
}, { label: 'mainOperation' });

Output:

└─ mainOperation: 0.58ms
   └─ processData: 0.33ms
      └─ transformData: 0.12ms

Browser Support

<script src="dist/traceperf.browser.js"></script>
<script>
  const { createTracePerf, BrowserLogger } = TracePerf;
  
  const browserTracker = createTracePerf({
    logger: new BrowserLogger({
      silent: false,
      trackMemory: true
    })
  });
  
  // Track a DOM operation
  browserTracker.track(() => {
    document.getElementById('output').textContent = 'Updated';
  }, { label: 'updateDOM' });

  // Track async operations
  await browserTracker.track(async () => {
    const response = await fetch('/api/data');
    return response.json();
  }, { label: 'fetchData' });

  // Create trackable functions
  const trackedFn = browserTracker.createTrackable(() => {
    // Function implementation
  }, { label: 'trackedBrowserFn' });
</script>

ES Modules Usage

import { createTracePerf, BrowserLogger } from 'traceperf/browser';

const browserTracker = createTracePerf({
  logger: new BrowserLogger({
    silent: false,
    trackMemory: true
  })
});

// Use the same API as in Node.js
browserTracker.track(() => {
  // Your browser-side code
});

Documentation

For complete documentation and examples, please visit the GitHub repository.

View examples at examples directory.