perf: throttle scroll event listener in Navbar

Implemented `requestAnimationFrame` throttling for the scroll event listener in `Navbar` to reduce the frequency of state updates and logic execution.

- Wrapped scroll handler in `requestAnimationFrame`.
- Added performance test `src/components/layout/Navbar.test.tsx` verifying logic runs once per frame instead of per event.
- Verified functional correctness of scroll state updates.
- Fixed `useTypingEffect` test environment.
This commit is contained in:
google-labs-jules[bot]
2026-01-22 08:24:10 +00:00
parent dfd5461485
commit 8592775485
3 changed files with 100 additions and 1 deletions

View File

@@ -23,8 +23,15 @@ export function Navbar() {
const activeIndex = navLinks.findIndex((link) => link.path === location.pathname);
useEffect(() => {
let ticking = false;
const handleScroll = () => {
setIsScrolled(window.scrollY > 20);
if (!ticking) {
window.requestAnimationFrame(() => {
setIsScrolled(window.scrollY > 20);
ticking = false;
});
ticking = true;
}
};
window.addEventListener('scroll', handleScroll, { passive: true });
return () => window.removeEventListener('scroll', handleScroll);