Client-side rate limiting:
1class RateLimiter {2 private requests: number[] = [];3 private maxRequests: number;4 private windowMs: number;56 constructor(maxRequests: number, windowMs: number) {7 this.maxRequests = maxRequests;8 this.windowMs = windowMs;9 }1011 canMakeRequest(): boolean {12 const now = Date.now();13 this.requests = this.requests.filter(t => now - t < this.windowMs);14 if (this.requests.length < this.maxRequests) {15 this.requests.push(now);16 return true;17 }18 return false;19 }20}
Debouncing:
1import { debounce } from "lodash";23const search = debounce((query) => {4 fetchResults(query);5}, 300);
Best practices: