Last week I release Gumshoe, a lightweight, framework-free script that highlights the current content in the navigation area on single-page sites.
I used a few tricks in Gumshoe to make it more performant:
- Navigation links and their associated content are stored in an array when the script is initialized. This means that the script doesn’t have to search the DOM for them on scroll.
- I also stored all measurements—things like how tall the document is and how far from the top sections are—to variables on initialization so the script doesn’t need to do as many calculations on the fly.
- Like all of my scripts that use scroll and resize events, I’ve added an event throttler to prevent methods from being called an absurd number of times during the course of an event.