Debounce
javascript
1function debounce(fn, ms) {2 let timer;3 return (...args) => {4 clearTimeout(timer);5 timer = setTimeout(() => fn(...args), ms);6 };7}
Throttle
javascript
1function throttle(fn, ms) {2 let last = 0;3 return (...args) => {4 const now = Date.now();5 if (now - last >= ms) {6 last = now;7 fn(...args);8 }9 };10}
Memoization
javascript
1function memoize(fn) {2 const cache = new Map();3 return (...args) => {4 const key = JSON.stringify(args);5 if (!cache.has(key)) cache.set(key, fn(...args));6 return cache.get(key);7 };8}
Заключение
Оптимизация — баланс между производительностью и читаемостью.
Изучите вопросы по JavaScript в нашем разделе.