hi@shahlol.ly

Quick Performance Hacks for Faster Websites

Performance Optimization January 02, 2025 3 min read

Every millisecond matters when it comes to the web. A sluggish website results in decreased conversions, higher bounce rates, and a bad user experience. But the good news? You don’t need a complete rewrite to see improvements! Here are quick and effective performance hacks to speed up your frontend web apps.




Use Lighter Libraries & Avoid Bloat πŸ› οΈ


Your app might already be carrying unnecessary weight due to large or outdated libraries. Reducing bundle size is an easy win for performance.

Check Your Dependencies

Every package you use adds to your bundle size. Some libraries, while useful, might be too heavy or not optimized for modern bundlers.

πŸ” Example: (Minified + Gzipped bundle)

  • ❌ moment.js (73.1kB) β€” suitable for backend applications
  • βœ… date-fns (17.5kB) β€” lightweight & tree-shakable
  • βœ… dayjs (3kB) β€” moment.js replacement for performance

How to Deal With Dependencies
  • Run webpack-bundle-analyzer or vite-bundle-visualizer to spot unexpectedly large imports in your project.

  • Use Bundlephobia (bundlephobia.com) to check the size of a package before installing it.


Tip: Opt-in for ESM-based libraries that support treeshaking to avoid unnecessary code.




Optimize with Treeshaking & Code Splitting βœ‚οΈ


Even if you choose lightweight libraries, your built code may still include some unused code. We can use Tree shaking and code splitting to ensure that browsers only download what's necessary.

Tree shaking:

Modern JavaScript bundlers e.g. Webpack can eliminate unused codeβ€”but only if the libraries you use support it.

For Tree shaking try the following:

  • βœ… Use ESM (ECMAScript Modules): By all means avoid using CommonJS (CJS), it's for server side.
  • βœ… Import Only What You Need: Instead of importing an entire library, grab only the functions you use named imports.

πŸ” Example:

  • ❌ Bad: Importing everything (bloats the bundle)
    import _ from "lodash";
    console.log(_.cloneDeep(obj));

  • βœ… Good: Importing only what's needed
    import cloneDeep from "lodash/cloneDeep";
    console.log(cloneDeep(obj));

Code Splitting: Load Only When Needed

Instead of loading everything at once, split your code into chunks so the browser loads them only when required and this will save on network loading and browser parsing times.

  • Dynamic Imports: Load code on demand instead of at startup (e.g. Using Intersection Observer, DOM event, etc.).
  • Route-based Splitting (aka lazy loading): Split your app into smaller chunks based on pages or route groups.

πŸ” Example:

Dynamic Import for Lazy Loading React pages


import { lazy, Suspense } from "react";
const HeavyComponent = lazy(() => import("./HeavyComponent"));

function App() {
  return (
    <Suspense fallback={<div>Loading...</div>}>
      <HeavyComponent />
    </Suspense>
  );
}

Tip: Always use lazy loading for non-essential components to keep your initial load fast.


Optimize Network Requests & Images πŸ–ΌοΈ


Slow network requests and unoptimized images cripple web performance. Optimizing these can significantly improve page load speeds hence user experience.



Reduce Network Requests

Each request adds latency, so reducing them speeds up page rendering.


Here's what you can do:

  • Minimize HTTP Requests: Less images, styles & code files means faster loads.
  • Use HTTP Caching: Set proper cache-control headers to reduce redundant fetches.
  • Preload Critical Assets: Use <link rel="preload" /> to prioritize important resources such LCP image, fonts, etc.

πŸ” Example:

Preloading a Font for Faster Rendering

<link rel="preload" href="/fonts/roboto.woff2" as="font"
      type="font/woff2" crossorigin="anonymous">

Lazy Loading an Image
<img src="image.webp" width="600" height="400" loading="lazy" alt="Optimized Image">

Tip: Always provide width and height for images to improve CLS and use lazy loading for non-visible assets.


Β© 2025 Abdelsalam Shahlol.

All rights reserved.