DEV Community

Discussion on: Nav-link's to active as you scroll through sections, in 10 Lines of JavaScript;

Collapse
 
greggcbs profile image
GreggHume • Edited

Good concept, i wonder if there is a better way to do this other than to forEach all elements twice on every scroll.

Adding a throttle will help. The amount of executions the above is making is a crazy amount per scroll.

*update: i did some performance benchmarks, it looks like using throttle or debounce is less performant than letting the function run on every scroll.

I opened an issue here for informational purposes:
github.com/niksy/throttle-debounce...