-
Notifications
You must be signed in to change notification settings - Fork 22
Home
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.
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.
- 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.
- 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.
To install Performance Decorators, run the following command in your project directory:
npm install performance-decorators
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
}
}
-
Description: Logs the time taken by the decorated method.
-
Usage:
import { LogExecutionTime } from "performance-decorators/debugging"; class Example { @LogExecutionTime() compute() { // Perform some computation } }
-
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 } }
-
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 } }
-
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!"); } }
-
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 }
-
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); } }
-
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}`); } }
-
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); } }
-
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); } }
-
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(); } }
-
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)); } }
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
}
}
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);
}
}
Contributions are welcome! If you'd like to contribute, please follow these guidelines:
- Fork the Repository: Click the 'Fork' button at the top right of this page.
- Clone Your Fork: Find the URL of your forked repo and clone it to your development environment.
- Create a New Branch: Use a branch name that describes your contribution.
- Make Your Changes: Add your changes to your branch.
- Run the Tests: Ensure all tests pass and add any new ones as needed.
- 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.
This project is licensed under the MIT License - see the LICENSE file for details.