Skip to content
Ryan Myrvold edited this page May 12, 2024 · 1 revision

Welcome to the Performance Decorators Wiki!

Performance Decorators is a TypeScript-based toolkit designed to simplify performance monitoring and optimization in Node.js and browsers. This Wiki provides detailed documentation, usage guides, and examples to help you get the most out of Performance Decorators.


Table of Contents

  1. Introduction
  2. Features
  3. Getting Started
  4. Decorator Documentation
  5. Examples
  6. Contributing
  7. License

Introduction

Performance Decorators is designed to help developers track performance bottlenecks and optimize their applications efficiently. With a suite of method and class decorators, you can easily add performance monitoring and enhancements directly to your code.


Features

Debugging Decorators

  • LogExecutionTime: Logs the execution time of methods, helping identify slow operations.
  • WarnPerformanceThreshold: Alerts when method execution times exceed a specified threshold.
  • LogMemoryUsage: Tracks and logs memory usage before and after method execution.
  • LogMethodError: Logs errors and optionally rethrows them when methods fail.
  • MemoryLeakWarning: Monitors potential memory leaks in class instances over time.

Optimization Decorators

  • BatchOperations: Batches multiple calls into a single asynchronous operation to minimize redundant processing.
  • Debounce: Limits the rate at which a function can be called, reducing the number of times it fires in quick succession.
  • Memoize: Caches the results of expensive function calls to avoid redundant computations.
  • Throttle: Ensures a function is not executed more than once within a specified time frame.
  • AutoRetry: Automatically retries failed asynchronous operations until they succeed or reach a retry limit.
  • LazyLoad: Delays the initialization of properties until they are first accessed, improving startup performance.

Getting Started

Installation

To install Performance Decorators, run the following command in your project directory:

npm install performance-decorators

Basic Usage

Here is a quick example to show how to use the LogExecutionTime decorator:

import { LogExecutionTime } from "performance-decorators/debugging";

class Example {
  @LogExecutionTime()
  compute() {
    // Perform some computation
  }
}

Decorator Documentation

Debugging Decorators

LogExecutionTime

  • Description: Logs the time taken by the decorated method.

  • Usage:

    import { LogExecutionTime } from "performance-decorators/debugging";
    
    class Example {
      @LogExecutionTime()
      compute() {
        // Perform some computation
      }
    }

WarnPerformanceThreshold

  • Description: Emits a warning if the decorated method's execution time exceeds a specified threshold.

  • Usage:

    import { WarnPerformanceThreshold } from "performance-decorators/debugging";
    
    class Example {
      @WarnPerformanceThreshold(200)
      compute() {
        // Perform some computation
      }
    }

LogMemoryUsage

  • Description: Logs the memory usage before and after the execution of the decorated method.

  • Usage:

    import { LogMemoryUsage } from "performance-decorators/debugging";
    
    class Example {
      @LogMemoryUsage()
      compute() {
        // Perform some computation
      }
    }

LogMethodError

  • Description: Logs any error that occurs during the execution of the decorated method and optionally rethrows it.

  • Usage:

    import { LogMethodError } from "performance-decorators/debugging";
    
    class Example {
      @LogMethodError(true)
      compute() {
        throw new Error("Something went wrong!");
      }
    }

MemoryLeakWarning

  • Description: Monitors and logs potential memory leaks by checking the memory usage over time.

  • Usage:

    import { MemoryLeakWarning } from "performance-decorators/debugging";
    
    @MemoryLeakWarning(30000, 20)
    class Example {
      // Class implementation
    }

Optimization Decorators

BatchOperations

  • Description: Batches multiple calls into a single asynchronous operation.

  • Usage:

    import { BatchOperations } from "performance-decorators/optimization";
    
    class Renderer {
      @BatchOperations()
      render(changes: any[][]) {
        console.log("Applying batch changes:", changes);
      }
    }

Debounce

  • Description: Prevents the decorated method from being called more than once within a specified delay.

  • Usage:

    import { Debounce } from "performance-decorators/optimization";
    
    class SearchComponent {
      @Debounce(300)
      onSearch(term: string) {
        console.log(`Searching for: ${term}`);
      }
    }

Memoize

  • Description: Caches and returns the result of expensive function calls based on arguments.

  • Usage:

    import { Memoize } from "performance-decorators/optimization";
    
    class Calculator {
      @Memoize()
      fibonacci(n: number): number {
        if (n <= 1) return n;
        return this.fibonacci(n - 1) + this.fibonacci(n - 2);
      }
    }

Throttle

  • Description: Ensures that the decorated method is not called more than once within a specified time frame.

  • Usage:

    import { Throttle } from "performance-decorators/optimization";
    
    class ScrollHandler {
      @Throttle(100)
      onScroll(event: Event) {
        console.log('Scrolling', event);
      }
    }

AutoRetry

  • Description: Automatically retries the decorated asynchronous method until it succeeds or reaches the maximum number of retries.

  • Usage:

    import { AutoRetry } from "performance-decorators/optimization";
    
    class DataService {
      @AutoRetry(3, 1000)
      async fetchData(url: string) {
        console.log(`Fetching data from ${url}`);
        const response = await fetch(url);
        if (!response.ok) {
          throw new Error('Network response was not ok.');
        }
        return response.json();
      }
    }

LazyLoad

  • Description: Delays the initialization of the property value until the first time it is accessed.

  • Usage:

    import { LazyLoad } from "performance-decorators/optimization";
    
    class ExpensiveComputation {
      @LazyLoad()
      get expensiveData() {
        console.log("Computing expensive data");
        return Array.from({ length: 1000000 }, (_, i) => Math.sqrt(i));
      }
    }

Examples

Using Debugging Decorators

Here’s how you can use the LogExecutionTime and LogMemoryUsage decorators:

import { LogExecutionTime, LogMemoryUsage } from "performance-decorators/debugging";

class Example {
  @LogExecutionTime()
  @LogMemoryUsage()
  compute() {
    // Your computation here
  }
}

Using Optimization Decorators

Here’s how you can use the Debounce and Memoize decorators:

import { Debounce, Memoize } from "performance-decorators/optimization";

class SearchComponent {
  @Debounce(300)
  onSearch(term: string) {
    console.log(`Searching for: ${term}`);
  }
}

class Calculator {
  @Memoize()
  fibonacci(n: number): number {
    if (n <= 1) return n;
    return this.fibonacci(n - 1) + this.fibonacci(n - 2);
  }
}

Contributing

Contributions are welcome! If you'd like to contribute, please follow these guidelines:

  1. Fork the Repository: Click the 'Fork' button at the top right of this page.
  2. Clone Your Fork: Find the URL of your forked repo and clone it to your development environment.
  3. Create a New Branch: Use a branch name that describes your contribution.
  4. Make Your Changes: Add your changes to your branch.
  5. Run the Tests: Ensure all tests pass and add any new ones as needed.
  6. Submit a Pull Request: Push your branch and changes to your fork on GitHub and submit a pull request.

Please include unit tests and documentation for any new features or changes. Follow the existing style to keep the code clean and readable.

License

This project is licensed under the MIT License - see the LICENSE file for details.