Skip to content

Conversation

@Bitshifter-9
Copy link

@Bitshifter-9 Bitshifter-9 commented Dec 2, 2025

Summary of Changes

Fixed a memory leak in the onLoadById utility function that could cause intervals to run indefinitely when DOM elements are not found.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

The Problem

The onLoadById function in src/utils/scrollingOperations.js creates a setInterval that checks every 100ms for a DOM element to appear. However, if the element never appears (e.g., when a user navigates away before the page fully loads), the interval continues running indefinitely.

Why the old code was risky:

  • Memory leak: The interval object and its closure remain in memory forever
  • CPU waste: Unnecessary DOM queries every 100ms with no way to stop
  • Performance degradation: Multiple abandoned intervals can accumulate over time during navigation

Affected code path: This function is used in LeftNav.jsx when navigating to the "How it Works" section from other pages.


The Solution

Added a timeout mechanism that automatically clears the interval after 5 seconds (50 attempts × 100ms) if the element is not found:

Changes made:

  1. Added maxAttempts constant to limit interval execution
  2. Track the number of attempts with a counter
  3. Clear the interval when max attempts are reached
  4. Improved JSDoc comments with proper @param annotations

How the new code handles it:

  • ✅ Normal case: Element found quickly → callback executes, interval clears (no change in behavior)
  • ✅ Edge case: Element never found → interval auto-clears after 5 seconds (prevents memory leak)

Testing

Code Validation

  • ✅ JavaScript syntax validated with Node.js (node -c)
  • ✅ Logic reviewed for correctness
  • ✅ Timeout calculation verified (50 × 100ms = 5 seconds)

Manual Testing Recommended

After merge, please verify:

  1. Navigate to a page other than home
  2. Click "How it Works" in the navigation menu
  3. Verify smooth scrolling to the section works correctly
  4. Check browser console for any errors

Additional Notes

  • This is a defensive fix that only affects the failure path
  • No changes to the public API or function signature
  • Existing behavior is preserved for successful cases
  • Follows best practices for interval management in JavaScriptAdd timeout mechanism to prevent setInterval from running indefinitely when the target element is never found. The function now stops checking after 5 seconds (50 attempts * 100ms) to prevent memory leaks when users navigate away before the element loads.

Also improved JSDoc comments to use proper @param annotations.

Add timeout mechanism to prevent setInterval from running indefinitely
when the target element is never found. The function now stops checking
after 5 seconds (50 attempts * 100ms) to prevent memory leaks when users
navigate away before the element loads.

Also improved JSDoc comments to use proper @param annotations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant