React Performance & Optimization

Performance in React apps can be thought of in 2 ways:

  1. Loading Performance - Compressing & loading code/assets (mostly Non-React things)
  2. Runtime Performance - CPU & rendering issues (mostly React-specific things)

Loading vs. Runtime

Loading Performance - This is a measure of how fast the content is loaded when a user visits your webpage. Some specific metrics are First Contentful Paint (FCP), Largest Contentful Paint (LCP), First Input Delay (FID), TTI (Time to Interactive), and maybe a "Speed Index".

Runtime Performance - This is a measure of how "smooth" your application runs and functions after the initial load. Some specific metrics for this might be "frame rate", "CPU", and "Memory Usage".

Loadtimes (Measure)

Loadtimes (Optimize)

Send as little code/media as possible over the network, and optimize everything

  • Utilize GZip compression server-side to compress all in-flight HTTP requests
  • Optimize all images and videos included in the bundle
  • Building and Minifying all project assets to CSS / JavaScript
  • Various Methods of Code Splitting
  • Server-Side Rendering (SSR) and Static Site Generation (SSG) can improve the First Contentful Paint and Time to Interactive metrics, as they allow the browser to render the page more quickly (and are accessible by search engines).
    • Adding SSR / SSG to an existing project is no small feat. Try to evaluate early on if your application would benefit from these technologies so you can configure your project the right way from the start.

Runtimes (Measure)

Modern React is pretty fast by default. Unless you're building complex components/features - you don't need to reach for optimization tactics until you notice a component/feature behaving slowly. The best advice to follow:

  1. Don't optimize too soon
  2. Know what to measure

In modern React apps, most of the performance issues you'll run into can probably be simplified down to to rendering problems (either too slow, or too much). The visual in the tweet below explains how rendering can cause cascading effects across a large application.

Read more from Alex Sidorenko in his series A Visual Guide to React Rendering.

Other helpful resources for understanding how to measure runtime performance in React apps:

Runtimes (Optimize)

Runtime performance issues usually boil down to two types of issues:

  1. Fixing Slow Renders
  1. Fixing Profuse Re-Renders
  • Use Refs for state & values that shouldn't cause re-renders (or that aren't rendered at all)
  • Be careful of setting state in an effect or outside of event-handlers / conditions

Memoization & Virtualization can solve slow renders or profuse re-renders in different ways:

Misc

More resources about optimizing JavaScript/React performance