We pulled in a utility library for exactly one function. Here is that function, written by hand and tested.
Start with the platform
Native modules and a small build step replaced the bundler configuration nobody could explain. The deploy got faster and the source maps finally line up.
export function debounce(fn, wait = 200) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), wait);
};
}
const search = document.querySelector('#s');
search.addEventListener('input', debounce((event) => {
console.log('Searching for', event.target.value);
}, 250));
Most of the interactions on our sites are a click handler and a class toggle. We write those by hand and keep them under a kilobyte. The snippet above is the whole thing; the version in our repository only adds tests.
Shipping less script
- Prefer a data attribute over a framework component
- Break long tasks into chunks of under 50 ms
- Load third-party scripts after interaction
- Type the public API even in plain JavaScript