// Performance optimization utilities import React from 'react'; // Lazy load component export function lazyLoad>( importFunc: () => Promise<{ default: T }> ): React.LazyExoticComponent { return React.lazy(importFunc); } // Debounce function for search/input export function debounce any>( func: T, wait: number ): (...args: Parameters) => void { let timeout: NodeJS.Timeout | null = null; return function executedFunction(...args: Parameters) { const later = () => { timeout = null; func(...args); }; if (timeout) { clearTimeout(timeout); } timeout = setTimeout(later, wait); }; } // Throttle function for scroll events export function throttle any>( func: T, limit: number ): (...args: Parameters) => void { let inThrottle: boolean; return function executedFunction(...args: Parameters) { if (!inThrottle) { func(...args); inThrottle = true; setTimeout(() => (inThrottle = false), limit); } }; } // Memoize expensive computations export function memoize( fn: (...args: Args) => Return ): (...args: Args) => Return { const cache = new Map(); return (...args: Args): Return => { const key = JSON.stringify(args); if (cache.has(key)) { return cache.get(key)!; } const result = fn(...args); cache.set(key, result); return result; }; } // Image preloader export function preloadImages(urls: string[]): Promise { return Promise.all( urls.map( (url) => new Promise((resolve, reject) => { const img = new Image(); img.onload = () => resolve(); img.onerror = reject; img.src = url; }) ) ); } // Batch API calls export function batchRequests( requests: (() => Promise)[], batchSize: number = 5 ): Promise { const results: T[] = []; const processBatch = async (batch: (() => Promise)[]) => { const batchResults = await Promise.all(batch.map((req) => req())); results.push(...batchResults); }; const processAll = async () => { for (let i = 0; i < requests.length; i += batchSize) { const batch = requests.slice(i, i + batchSize); await processBatch(batch); } }; return processAll().then(() => results); }