What is raf-schd?
The raf-schd package is a small utility that creates a throttled function that only invokes the provided function at most once per animation frame. It's useful for optimizing performance by reducing the number of times a function is called during rapid events such as scrolling, resizing, or animations.
Throttling function calls
This code sample demonstrates how to create a throttled version of an expensive function that is called at most once per animation frame. This is useful for attaching to events that fire rapidly, such as 'resize' or 'scroll' events.
import schedule from 'raf-schd';
const expensiveFunction = () => {
// Expensive operation
};
const throttledFunction = schedule(expensiveFunction);
window.addEventListener('resize', throttledFunction);
window.addEventListener('scroll', throttledFunction);