performance-toolkit v2.0.0
๐ PerformanceToolkit
A powerful JavaScript performance optimization toolkit that helps you build faster, more efficient applications.
๐ฏ Overview
PerformanceToolkit provides essential utilities for monitoring, measuring, and optimizing your JavaScript applications. From function memoization to parallel processing, this toolkit offers everything you need to unlock maximum performance.
โจ Key Features
- Advanced Memoization - Smart caching system for function results
- Performance Metrics - Detailed execution time tracking and analysis
- Parallel Processing - Multi-core CPU utilization for intensive tasks
- Smart Collections - Enhanced data structures with intelligent operations
- Rate Limiting - Built-in throttling and debouncing utilities
- Batch Processing - Efficient handling of large data sets
๐ฆ Installation
Using npm:
npm install performance-toolkit
Using yarn:
yarn add performance-toolkit
๐ Usage Guide
Basic Setup
import { PerformanceToolkit } from 'performance-toolkit';
const toolkit = new PerformanceToolkit();
Memoization
Cache expensive function results for improved performance:
const expensiveCalculation = (x, y) => {
// Complex calculation here
return x * y;
};
const memoizedCalc = toolkit.memoize(expensiveCalculation);
memoizedCalc(5, 10); // Computed
memoizedCalc(5, 10); // Retrieved from cache
Performance Measurement
Track execution times of your functions:
const processData = (data) => {
// Data processing logic
};
const measuredProcess = toolkit.measure(processData, 'dataProcessing');
measuredProcess(someData);
// Get performance metrics
const metrics = toolkit.getMetrics();
console.log(metrics.dataProcessing);
// Output: { avg: 1.23, min: 0.89, max: 1.45, count: 1 }
Parallel Processing
Distribute heavy computations across CPU cores:
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const processor = async (num) => {
// CPU-intensive task
return num * 2;
};
const results = await toolkit.parallelProcess(items, processor, {
batchSize: 2
});
Smart Collections
Enhanced array operations with intelligent features:
const collection = toolkit.createSmartCollection();
collection.push(10, 20, 30, 40, 50);
const result = collection.smartFind(x => x === 30);
console.log(result);
/* Output:
{
value: 30,
index: 2,
adjacent: {
prev: 20,
next: 40
}
}
*/
Rate Limiting
Control function execution frequency:
// Debouncing
const debouncedSave = toolkit.debounce(() => {
// Save operation
}, 1000);
// Throttling
const throttledUpdate = toolkit.throttle(() => {
// Update operation
}, 500);
Batch Processing
Handle large datasets efficiently:
const items = Array.from({ length: 1000 }, (_, i) => i);
const processor = async (item) => item * 2;
const results = await toolkit.batchProcess(items, processor, 100);
๐ Performance Monitoring
Track performance metrics across your application:
// After running various operations
const allMetrics = toolkit.getMetrics();
console.log(allMetrics);
/* Output:
{
"operation1": { avg: 1.2, min: 0.8, max: 1.5, count: 10 },
"operation2": { avg: 2.3, min: 1.9, max: 2.8, count: 5 }
}
*/
โ๏ธ Configuration Options
Parallel Processing Options
{
batchSize: 100, // Number of items to process in each batch
}
Memoization Options
// Custom key generator
const customKeyGen = (...args) => args.join('-');
const memoizedFn = toolkit.memoize(myFunction, customKeyGen);
๐งช Testing
The toolkit includes a comprehensive test suite ensuring reliability and correctness. Run tests using:
npm run test
๐ Performance Tips
- Optimal Batch Sizes: Adjust batch sizes based on your data characteristics and CPU capabilities
- Strategic Memoization: Memoize expensive operations but be mindful of memory usage
- Monitoring Metrics: Regularly check performance metrics to identify bottlenecks
- Rate Limiting: Use debouncing for user input and throttling for regular updates
๐ License
MIT License
๐ Credits
Made with โค๏ธ by Massachusetts
Note: For optimal performance, ensure your Node.js environment is configured to utilize all available CPU cores when using parallel processing features.