| 475 | * @returns Throttled function |
| 476 | */ |
| 477 | export function throttle<T extends (...args: unknown[]) => unknown>( |
| 478 | fn: T, |
| 479 | limit: number |
| 480 | ): (...args: Parameters<T>) => void { |
| 481 | let lastCall = 0; |
| 482 | let timeoutId: ReturnType<typeof setTimeout> | null = null; |
| 483 | |
| 484 | return (...args: Parameters<T>) => { |
| 485 | const now = Date.now(); |
| 486 | const remaining = limit - (now - lastCall); |
| 487 | |
| 488 | if (remaining <= 0) { |
| 489 | if (timeoutId) { |
| 490 | clearTimeout(timeoutId); |
| 491 | timeoutId = null; |
| 492 | } |
| 493 | lastCall = now; |
| 494 | fn(...args); |
| 495 | } else if (!timeoutId) { |
| 496 | timeoutId = setTimeout(() => { |
| 497 | lastCall = Date.now(); |
| 498 | timeoutId = null; |
| 499 | fn(...args); |
| 500 | }, remaining); |
| 501 | } |
| 502 | }; |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Estimate memory usage of an object (rough approximation) |