Skip to main content

Performance Optimization Guide

This guide covers various techniques to optimize the performance of your Refract applications.

Table of Contents

Memoization

Memoization can significantly improve performance by caching the results of expensive function calls.

import { memo } from 'react';

const ExpensiveComponent = memo(({ data }) => {
// Component implementation
});

Lazy Loading

Lazy load components to reduce the initial bundle size:

import { lazy } from 'react';

const LazyComponent = lazy(() => import('./LazyComponent'));

State Management

Use Local State When Possible

Keep state as local as possible to minimize re-renders.

Batch State Updates

Group multiple state updates together to reduce re-renders:

// Instead of:
setValue1(newValue1);
setValue2(newValue2);

// Do:
batch(() => {
setValue1(newValue1);
setValue2(newValue2);
});

Rendering Optimization

Use React.memo for Pure Components

const MyComponent = React.memo(({ value }) => {
return <div>{value}</div>;
});

Use useMemo for Expensive Calculations

const result = useMemo(() => {
return expensiveCalculation(deps);
}, [deps]);

Best Practices

  1. Avoid Inline Function Definitions in JSX
  2. Use React DevTools Profiler to identify performance bottlenecks
  3. Implement Virtualization for long lists
  4. Use Production Builds for performance testing

Tools

  • React DevTools
  • Lighthouse
  • Web Vitals