Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 16, 2025

The website was inefficient due to unthrottled scroll event handlers, blocking script execution, and missing font optimization.

Changes

JavaScript Performance

  • Throttled scroll handler to execute at 100ms intervals instead of every scroll event (~98% reduction in executions)
  • Added passive: true flag to scroll listener for non-blocking scroll
  • Cached window.innerHeight to eliminate redundant DOM queries
  • Fixed missing fade-in animation by implementing CSS for visible class
// Before: Executes on every scroll event (50+ times/sec)
window.addEventListener('scroll', () => {
    document.querySelectorAll('section').forEach(section => {
        const rect = section.getBoundingClientRect();
        if(rect.top < window.innerHeight - 50) {
            section.classList.add('visible');
        }
    });
});

// After: Throttled to 10 times/sec with passive listener
const handleScroll = throttle(() => {
    const sections = document.querySelectorAll('section');
    const windowHeight = window.innerHeight;
    sections.forEach(section => {
        const rect = section.getBoundingClientRect();
        if(rect.top < windowHeight - 50) {
            section.classList.add('visible');
        }
    });
}, 100);

window.addEventListener('scroll', handleScroll, { passive: true });

Resource Loading

  • Added preconnect hints for Google Fonts to eliminate DNS/connection latency
  • Added display=swap to font import to prevent FOUT
  • Added defer attribute to script tag for non-blocking execution

CSS

  • Implemented fade-in/slide-up transitions for sections entering viewport
  • Removed redundant comments

Screenshots

Initial load:
Homepage

Scroll animation in action:
Projects Section

Original prompt

I would like to make this website more efficient.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: techdiwas <209884248+techdiwas@users.noreply.github.com>
@techdiwas techdiwas marked this pull request as ready for review December 16, 2025 16:46
Copilot AI review requested due to automatic review settings December 16, 2025 16:46
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements performance optimizations for a portfolio website, focusing on reducing JavaScript overhead, improving font loading efficiency, and enhancing the user experience with smooth animations.

Key changes:

  • Implemented throttling for scroll event handlers to reduce execution frequency by ~98%
  • Added font preloading and preconnect hints for Google Fonts
  • Enhanced CSS with fade-in animations for viewport sections

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
index.html Added Google Fonts preconnect links and Poppins font import; added defer attribute to script tag
script.js Implemented throttle function and refactored scroll handler to cache DOM queries and use passive event listeners
style.css Added fade-in animation styles for sections and removed redundant comments

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

</footer>

<script src="script.js"></script>
<script src="script.js" defer></script>
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The defer attribute is positioned after the closing </footer> tag at the end of the body. Since the script is already at the end of the body, defer provides no additional benefit and may cause the script to execute after DOMContentLoaded, potentially delaying the initial handleScroll() call. Consider removing defer or moving the script to the <head> if you want to use defer for parallel loading.

Suggested change
<script src="script.js" defer></script>
<script src="script.js"></script>

Copilot uses AI. Check for mistakes.
Comment on lines +13 to +15
// Simple scroll fade-in with throttling
const handleScroll = throttle(() => {
const sections = document.querySelectorAll('section');
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sections variable is queried on every throttled scroll event. Since sections are static DOM elements that don't change after page load, this query should be moved outside the handleScroll function and cached at the module level to avoid repeated DOM queries.

Suggested change
// Simple scroll fade-in with throttling
const handleScroll = throttle(() => {
const sections = document.querySelectorAll('section');
// Cache sections NodeList at the module level
const sections = document.querySelectorAll('section');
// Simple scroll fade-in with throttling
const handleScroll = throttle(() => {

Copilot uses AI. Check for mistakes.
Copilot AI changed the title [WIP] Refactor code for improved website efficiency Optimize website performance with throttled scroll handlers and async font loading Dec 16, 2025
Copilot AI requested a review from techdiwas December 16, 2025 16:48
Copy link
Owner

@techdiwas techdiwas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good I guess 😁💯.

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.

2 participants