Skip to main content
Course/Module 5/Topic 4 of 4Advanced

Performance Best Practices

Learn advanced performance optimization techniques and best practices for React applications

85 minBy Priygop TeamLast updated: Feb 2026

Performance Best Practices

  • Use React DevTools Profiler to identify bottlenecks
  • Implement virtualization for large lists
  • Optimize images and assets
  • Use web workers for heavy computations
  • Implement proper error boundaries
  • Use production builds for testing

Virtualization Example

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

// Virtualized list for large datasets
function VirtualizedList({ items }) {
    const Row = ({ index, style }) => (
        <div style={style}>
            <div className="row">
                <span>{items[index].name}</span>
                <span>{items[index].email}</span>
            </div>
        </div>
    );

    return (
        <List
            height={400}
            itemCount={items.length}
            itemSize={50}
            width="100%"
        >
            {Row}
        </List>
    );
}

// Web Worker for heavy computations
// worker.js
self.onmessage = function(e) {
    const { data, type } = e.data;
    
    if (type === 'HEAVY_CALCULATION') {
        const result = performHeavyCalculation(data);
        self.postMessage({ type: 'RESULT', result });
    }
};

function performHeavyCalculation(data) {
    // Simulate heavy computation
    let result = 0;
    for (let i = 0; i < 1000000; i++) {
        result += Math.sqrt(i);
    }
    return result;
}

// Using web worker in React
function HeavyCalculationComponent() {
    const [result, setResult] = useState(null);
    const [loading, setLoading] = useState(false);

    const performCalculation = useCallback((data) => {
        setLoading(true);
        
        const worker = new Worker('/worker.js');
        
        worker.onmessage = function(e) {
            if (e.data.type === 'RESULT') {
                setResult(e.data.result);
                setLoading(false);
            }
        };
        
        worker.postMessage({ data, type: 'HEAVY_CALCULATION' });
    }, []);

    return (
        <div>
            <button onClick={() => performCalculation([1, 2, 3, 4, 5])}>
                Start Heavy Calculation
            </button>
            {loading && <p>Calculating...</p>}
            {result && <p>Result: {result}</p>}
        </div>
    );
}

Error Boundaries

Example
// Error Boundary Component
class ErrorBoundary extends React.Component {
    constructor(props) {
        super(props);
        this.state = { hasError: false, error: null };
    }

    static getDerivedStateFromError(error) {
        return { hasError: true, error };
    }

    componentDidCatch(error, errorInfo) {
        // Log error to service
        logErrorToService(error, errorInfo);
    }

    render() {
        if (this.state.hasError) {
            return (
                <div className="error-boundary">
                    <h2>Something went wrong.</h2>
                    <button onClick={() => this.setState({ hasError: false })}>
                        Try again
                    </button>
                </div>
            );
        }

        return this.props.children;
    }
}

// Using Error Boundaries
function App() {
    return (
        <ErrorBoundary>
            <Header />
            <ErrorBoundary>
                <MainContent />
            </ErrorBoundary>
            <Footer />
        </ErrorBoundary>
    );
}

Try It Yourself — Performance Optimization

Try It Yourself — Performance OptimizationHTML
HTML Editor
✓ ValidTab = 2 spaces
HTML|53 lines|2145 chars|✓ Valid syntax
UTF-8

Quick Quiz — Performance Optimization

Additional Resources

Recommended Reading

  • React Performance Documentation
  • React DevTools Profiler Guide
  • Code Splitting Best Practices

Online Resources

  • React Performance Tutorial
  • React.memo Examples
  • Code Splitting Guide
Chat on WhatsApp
Priygop - Leading Professional Development Platform | Expert Courses & Interview Prep