Optimizing Front-End Performance With React in 2025: Practical Guide Based on Core Web Vitals

  • Home /
  • Optimizing front-end performance with React in 2025: practical guide based on Core Web Vitals

React Performance Guide

Optimizing front-end performance with React in 2025: practical guide based on Core Web Vitals

Introduction

In 2025, web performance is no longer a “bonus”: it determines user experience and SEO ranking. Google uses Core Web Vitals as key indicators in its ranking, and in React, it’s easy to end up with a heavy bundle, unnecessary re-renders, and a slow app.

But performance optimization goes far beyond that:

  • It directly improves user satisfaction (reduced loading time = less abandonment).
  • It impacts SEO and visibility on search engines.
  • It reduces infrastructure costs (fewer resources consumed).

👉 The good news? Many performance problems can be quickly fixed with simple good practices. Let’s see how.


Understanding Core Web Vitals in 2025

Essential metrics

  • LCP (Largest Contentful Paint) → time to display the main element (target < 2.5s).
  • INP (Interaction to Next Paint) → replaces FID, measures overall responsiveness (target < 200ms).
  • CLS (Cumulative Layout Shift) → visual stability, no shifts (target < 0.1).
  • TTI (Time to Interactive) → when the page becomes truly usable.
  • TTFB (Time to First Byte) → server speed.

These metrics are measurable via Lighthouse, PageSpeed Insights and the web-vitals package.


Step 1 – Measure before optimizing

1. Lighthouse (Chrome DevTools)

  • Open DevTools → Lighthouse tab → launch Performance audit.
  • Check LCP, INP, CLS and recommendations.

2. PageSpeed Insights

  • Test your page on mobile + desktop.
  • Provides a report based on real Chrome UX Report data.

3. web-vitals package

Log Web Vitals directly in your React app:

import { onCLS, onINP, onLCP } from 'web-vitals';

onCLS(console.log);
onINP(console.log);
onLCP(console.log);

4. Production monitoring

Integrate metrics into your monitoring system for continuous tracking.


Step 2 – Optimize initial loading (LCP)

1. Code splitting with React.lazy

const Dashboard = React.lazy(() => import('./Dashboard'));

function App() {
  return (
    <React.Suspense fallback={<p>Loading…</p>}>
      <Dashboard />
    </React.Suspense>
  );
}

2. Lazy loading images

<img src="image.webp" alt="Product" loading="lazy" width="600" height="400" />

3. Modern formats (WebP, AVIF)

<picture>
  <source srcSet="image.avif" type="image/avif" />
  <source srcSet="image.webp" type="image/webp" />
  <img src="image.jpg" alt="Product" loading="lazy" />
</picture>

4. Use a CDN

  • Serve static assets (images, JS, CSS) via a CDN.
  • Reduce TTFB thanks to distributed cache.

Step 3 – Improve responsiveness (INP)

1. Avoid unnecessary re-renders with React.memo

React.memo is a Higher-Order Component (HOC) that memoizes the result of a component’s render. It only re-renders the component if its props have changed, which can significantly improve performance.

When to use React.memo?

  • Components that often receive the same props
  • Computationally expensive components
  • Child components that don’t need to re-render when the parent changes

Basic example

const Button = React.memo(({ onClick, label }) => {
  console.log('Button rendered'); // Only logs when props change
  return <button onClick={onClick}>{label}</button>;
});

// Usage
function App() {
  const [count, setCount] = useState(0);
  const [name, setName] = useState('John');
  
  const handleClick = useCallback(() => {
    console.log('Button clicked');
  }, []);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      {/* This Button does NOT re-render when count changes */}
      <Button onClick={handleClick} label="Static Button" />
    </div>
  );
}

Custom comparison with React.memo

By default, React.memo performs a shallow comparison. For complex props, you can provide a custom comparison function:

const UserCard = React.memo(({ user, settings }) => {
  return (
    <div>
      <h3>{user.name}</h3>
      <p>{user.email}</p>
      <span>Theme: {settings.theme}</span>
    </div>
  );
}, (prevProps, nextProps) => {
  // Return true if props are "equal" (no re-render)
  // Return false if props have changed (re-render needed)
  return (
    prevProps.user.id === nextProps.user.id &&
    prevProps.settings.theme === nextProps.settings.theme
  );
});

Pitfall to avoid: inline objects and functions

// ❌ Bad: creates a new object on every render
function App() {
  return <UserCard user={{ name: 'John', id: 1 }} />;
}

// ✅ Good: stable object
function App() {
  const user = useMemo(() => ({ name: 'John', id: 1 }), []);
  return <UserCard user={user} />;
}

Measuring the impact

Use React DevTools Profiler to measure React.memo’s impact:

// Before React.memo
const ExpensiveComponent = ({ data }) => {
  const processedData = data.map(item => heavyCalculation(item));
  return <div>{processedData.length} items processed</div>;
};

// After React.memo
const ExpensiveComponent = React.memo(({ data }) => {
  const processedData = useMemo(
    () => data.map(item => heavyCalculation(item)),
    [data]
  );
  return <div>{processedData.length} items processed</div>;
});

2. useMemo / useCallback

const filtered = useMemo(() => items.filter(i => i.active), [items]);
const handleClick = useCallback(() => console.log('click'), []);

3. List virtualization

import { FixedSizeList as List } from 'react-window';

<List
  height={400}
  itemCount={1000}
  itemSize={35}
  width={300}
>
  {({ index, style }) => (
    <div style={style} className="list-item">
      Item {index}
    </div>
  )}
</List>

Note: In this specific case, style={style} is necessary because react-window uses it for dynamic positioning of virtualized elements.

4. Split heavy components

  • Break into smaller pieces.
  • Load on demand (dynamic import).

Step 4 – Limit visual shifts (CLS)

1. Reserve image space

Always define width and height or use aspect-ratio.

img {
  aspect-ratio: 16 / 9;
}

2. Use skeleton loaders

Skeleton Loaders are visual placeholders that mimic the structure of the final content during loading. They significantly improve perceived performance and reduce CLS (Cumulative Layout Shift).

Why use Skeleton Loaders?

  • Performance perception: Users immediately see that something is loading
  • CLS reduction: Space is reserved, avoiding layout shifts
  • Better UX: More pleasant than a spinner or blank screen
  • Visual guidance: Users anticipate the type of content that will appear

Basic example

function Skeleton() {
  return (
    <div className="skeleton-container">
      <div className="skeleton skeleton-text skeleton-title" />
      <div className="skeleton skeleton-text skeleton-subtitle" />
      <div className="skeleton skeleton-image" />
      <div className="skeleton skeleton-text skeleton-paragraph" />
      <div className="skeleton skeleton-text skeleton-paragraph short" />
    </div>
  );
}

CSS for animations

.skeleton {
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: loading 1.5s infinite;
  border-radius: 4px;
}

@keyframes loading {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.skeleton-text {
  height: 16px;
  margin-bottom: 8px;
}

.skeleton-title {
  height: 24px;
  width: 70%;
}

.skeleton-subtitle {
  height: 18px;
  width: 50%;
}

.skeleton-image {
  height: 200px;
  width: 100%;
  margin: 16px 0;
}

.skeleton-paragraph {
  height: 16px;
  width: 100%;
}

.skeleton-paragraph.short {
  width: 60%;
}

Adaptive skeleton based on content

function UserCardSkeleton() {
  return (
    <div className="user-card">
      <div className="skeleton skeleton-avatar" />
      <div className="user-info">
        <div className="skeleton skeleton-name" />
        <div className="skeleton skeleton-email" />
        <div className="skeleton skeleton-role" />
      </div>
    </div>
  );
}

function ProductCardSkeleton() {
  return (
    <div className="product-card">
      <div className="skeleton skeleton-product-image" />
      <div className="skeleton skeleton-product-title" />
      <div className="skeleton skeleton-price" />
      <div className="skeleton skeleton-rating" />
    </div>
  );
}

Custom hook for skeleton management

function useSkeletonLoader(isLoading, data, SkeletonComponent) {
  if (isLoading || !data) {
    return <SkeletonComponent />;
  }
  return null;
}

// Usage
function UserProfile({ userId }) {
  const { data: user, isLoading } = useUser(userId);
  
  const skeleton = useSkeletonLoader(isLoading, user, UserCardSkeleton);
  if (skeleton) return skeleton;
  
  return <UserCard user={user} />;
}

Skeleton with React Suspense

function UserProfile({ userId }) {
  return (
    <Suspense fallback={<UserCardSkeleton />}>
      <UserData userId={userId} />
    </Suspense>
  );
}

function UserData({ userId }) {
  const user = use(fetchUser(userId)); // React 18+ use hook
  return <UserCard user={user} />;
}

Best practices for Skeletons

// ✅ Good: Skeleton that resembles the final content
function ArticleSkeleton() {
  return (
    <article className="article-skeleton">
      <div className="skeleton skeleton-title skeleton-width-80" />
      <div className="skeleton skeleton-meta skeleton-width-40" />
      <div className="skeleton skeleton-image skeleton-aspect-16-9" />
      <div className="skeleton skeleton-paragraph" />
      <div className="skeleton skeleton-paragraph" />
      <div className="skeleton skeleton-paragraph skeleton-width-70" />
    </article>
  );
}

// ❌ Bad: Generic skeleton that doesn't resemble anything
function BadSkeleton() {
  return <div className="skeleton skeleton-generic" />;
}
/* Reusable CSS classes for skeletons */
.skeleton-width-80 { width: 80%; }
.skeleton-width-70 { width: 70%; }
.skeleton-width-40 { width: 40%; }
.skeleton-aspect-16-9 { aspect-ratio: 16/9; }
.skeleton-generic { 
  width: 300px; 
  height: 200px; 
}

Measuring CLS impact

// Before: content that appears abruptly
function ProductList() {
  const { data: products, isLoading } = useProducts();
  
  if (isLoading) return <div>Loading...</div>; // ❌ High CLS
  
  return products.map(product => <ProductCard key={product.id} product={product} />);
}

// After: skeleton that reserves space
function ProductList() {
  const { data: products, isLoading } = useProducts();
  
  if (isLoading) {
    return (
      <div className="product-grid">
        {Array.from({ length: 6 }).map((_, i) => (
          <ProductCardSkeleton key={i} />
        ))}
      </div>
    ); // ✅ Reduced CLS
  }
  
  return (
    <div className="product-grid">
      {products.map(product => <ProductCard key={product.id} product={product} />)}
    </div>
  );
}

3. Avoid dynamic content injections

Plan the necessary space for content that loads asynchronously.


Step 5 – React-specific optimizations

1. Suspense + Streaming SSR (React 18)

With Next.js or Remix, you can stream HTML to the client to display content faster.

// Next.js App Router example
export default async function Page() {
  const data = await getData();
  return <div>{data.title}</div>;
}

2. React Server Components (RSC)

  • Avoids loading unnecessary code on the client side.
  • Drastically reduces JS bundle.

3. Concurrent Features

Use React 18’s new features to improve user experience.

4. Error boundaries with Suspense

function ErrorBoundary({ children }) {
  return (
    <ErrorBoundary fallback={<div>Loading error</div>}>
      <Suspense fallback={<Skeleton />}>
        {children}
      </Suspense>
    </ErrorBoundary>
  );
}

Performance PR checklist (React)

To check before each merge:

  • Optimized images (WebP/AVIF, lazy)
  • Code splitting (React.lazy, dynamic import)
  • Heavy components optimized (React.memo, useMemo, useCallback)
  • Virtualized lists if > 1000 elements
  • Space reservation for images/fonts
  • Lighthouse audit ≥ 90 in performance

Automated tools

  • Lint: eslint-plugin-react-hooks to detect performance issues.
  • Tests: Measure metrics in your integration tests.
  • CI: Run Lighthouse on key pages (target ≥ 90).

Express case study

Before: Lighthouse score = 45, LCP = 4.2s, INP = 350ms.

After optimizations (code splitting, WebP images, memoization, virtualization) → Score = 92, LCP = 1.8s, INP = 120ms.

Time invested: ~5 hours for massive gains on user experience.


Useful resources


Conclusion

Optimizing performance in React is not just a technical concern: it’s improving user experience, boosting SEO and retention, and reducing costs. By applying these simple optimizations, you:

  • make navigation easier for your users,
  • improve your SEO positioning,
  • and reduce infrastructure costs.

👉 Golden rule: Measure → Optimize → Measure again. Start by auditing your app with Lighthouse, fix two or three simple points (images, code splitting, memoization), and watch your Core Web Vitals climb.

Author Omar Ogoubi