DEV Community

Cover image for Respond to the interview question about React performance optimization. πŸš€
Herat Dhruv
Herat Dhruv

Posted on • Edited on

Respond to the interview question about React performance optimization. πŸš€

I have been involved in conducting and participating in React frontend interviews many times over recent years.

One observation I’ve made is that interviewers often ask about your experience with performance optimization. However, many candidates make the mistake of only discussing code-based optimizations, while interviewers are actually looking for your broader experience as a web developer, not just a React or framework-specific developer.

Consider performance optimization as a system design question and provide a holistic answer based on your web development expertise. I'll share with you the best possible response based on my experience, so you can be well-prepared if you're asked this in an interview. If you deliver the following points effectively, the interviewer will view you as a strong candidate for frontend or web development roles.

Let's break this answer down into six key segments:

πŸ‘¨πŸ»β€πŸ’» Code Performance Optimization
🌎 Network / API Layer Performance Optimization
πŸŒβ› Caching / CDN
πŸ—„οΈ Server Optimization
πŸ› οΈ Tools to Use
πŸ“Š Metrics to Follow

1. Code Performance Optimization

Here, we can discuss coding techniques that can improve performance. For example:

  • Hooks like useCallback, useMemo, and React.memo can provide caching mechanisms. However, we should be cautious not to overuse them, as this can negatively impact performance.
  • Techniques like React.lazy and Suspense can definitely improve performance by reducing the size of the main chunk, allowing for lazy loading.
  • Lazy loading images on a webpage can help the HTML load faster in the browser.
  • Pagination can also reduce the page size, thus improving performance.
  • Using tools like Webpack/Vite bundle analyzers can be useful in identifying heavy JS files, allowing us to reduce the file size on initial load.Resource

2. Network / API Layer Performance Optimization

  • When navigating through the code, you should identify:
  • Which API is "costly" or heavy (e.g., if the response is more than 9-10 MB). Such an API needs optimization.
  • Which API is slowβ€”consider adding server-side caching with tools like Redis or Memcached to get responses faster.
  • For example, if your product API response is too large and causes loading delays, this is unacceptable. You can reduce the response size by using pagination or lazy loading images.
  • A good practice would be to only send necessary data. For instance, in a "favourites" API, sending just the product_id to mark a product as a favourite is more efficient than sending the entire product object with all its properties.
  • Using GraphQL can also help adjust response sizes dynamically based on your needs.

3. Caching / CDN

  • As a frontend developer, we often serve assets like CSS, images, audio, or video, which can increase HTML page size and increase network retrieval times.
  • Rather than fetching these assets directly from the server, it’s always better to retrieve them via a CDN (Content Delivery Network), which can be provided by services like AWS, GCP, and other major players.

4. Server Optimization

  • React renders pages on the client side (i.e., in the browser), which uses client resources like CPU, processor, and network. While this may not be a big issue for desktop users, mobile users often have less computational power and slower network connections.
  • To improve performance for mobile users, Server-Side Rendering (SSR) can be a good solution. This allows major tasks like data fetching and HTML page generation to happen server-side, so the browser doesn’t have to wait for JavaScript files to load before rendering the HTML.
  • Other techniques include database optimization and applying ASG (Auto Scaling Group) policies on servers using load balancers during peak hours to distribute the load, enhancing web performance.

5. Tools to Use

Here are some tools you can use to analyze and enhance your website’s performance:

  • Lighthouse
  • Sitespeed
  • Pagespeed Insights
  • Webpage Test

These tools generate reports that provide actionable insights for improving the performance score of your website.

6. Metrics to Follow

Performance metrics to focus on include:

  • FCP (First Contentful Paint)
  • LCP (Largest Contentful Paint)
  • CLS (Cumulative Layout Shift)

It’s important to note that LCP, CLS, and TBT (Total Blocking Time) carry significant weight in performance scoring. Prioritize optimizing these metrics to eliminate performance bottlenecks on your site.

Here’s how the weightage typically breaks down:

Metrics Weights
Largest Contentful Paint (LCP) 25%
Total Blocking Time (TBT) 30%
Cumulative Layout Shift (CLS) 25%

Please make sure to cover all of these points, as each one holds weight in showcasing your overall experience.

Feel free to share your comments and feedback!

Top comments (0)