Performance
Try in Playground →Async utilities, benchmarking, and timing
Import
import { ... } from 'ts-time-utils/performance'; Examples
Timing & Benchmarks
Measure performance and control async timing
import { sleep, benchmark, Stopwatch, debounce, throttle } from 'ts-time-utils/performance';
// Sleep/delay
async function demo() {
console.log('Starting...');
await sleep(1000); // Wait 1 second
console.log('Done!');
}
// Benchmark a function
async function benchmarkDemo() {
const results = await benchmark(() => {
// Heavy operation
Array.from({ length: 10000 }, (_, i) => i * 2);
}, 100); // Run 100 times
console.log(`Average: ${results.average.toFixed(2)}ms`);
console.log(`Min: ${results.min.toFixed(2)}ms`);
console.log(`Max: ${results.max.toFixed(2)}ms`);
}
// Stopwatch for manual timing
const stopwatch = new Stopwatch();
stopwatch.start();
// ... do some work ...
stopwatch.lap(); // Record lap
// ... more work ...
console.log('Elapsed:', stopwatch.getElapsed(), 'ms');
console.log('Laps:', stopwatch.getLaps());
stopwatch.stop();
// Debounce (wait until calls stop)
const debouncedSave = debounce((data) => {
console.log('Saving:', data);
}, 300);
// Throttle (max once per interval)
const throttledLog = throttle((msg) => {
console.log(msg);
}, 1000);