Performance Profiling

Article · 2:00

Re-renders, dropped frames and memory - measuring them instead of guessing.

This lesson has 1 runnable example. Press Try yourself! under any of them to open it in the editor.

Mobile performance problems come in three shapes and each has its own tool. Unnecessary re-renders are the most common and the easiest to fix: a component rebuilds because its parent did, even though nothing it displays changed. Dropped frames are a thread missing its 16.7 millisecond budget - and knowing which thread matters, because a busy JavaScript thread with a smooth UI thread means your animations are fine and your logic is not. Memory growth that never comes back down is a leak, usually a listener or a timer that was never removed.

React Native DevTools is the current debugger, built on Chrome DevTools, opened with j in the Expo terminal. Its React Profiler records a session and shows which components rendered, how often, and why - the "why" is the part worth reading, because it names the prop that changed.

Fix re-renders in a specific order. Measure first. Then check that list rows are memoised and their callbacks stable. Then check your context providers, because a context whose value is a fresh object every render re-renders every consumer in the tree. useMemo on that value is frequently the single highest-value line in an app.

Flashlight scores performance on a real Android device, giving you a number to compare between builds rather than an impression. On iOS, Instruments still has the deepest view.

TS Example 1 39 lines
import { createContext, useContext, useMemo, useState, memo, useCallback } from 'react';
import { View, Text, Pressable } from 'react-native';

type Session = { userId: string; signOut: () => void };
const SessionContext = createContext<Session | null>(null);

// WRONG: a new object every render, so every consumer re-renders every time
// anything in this provider changes. The commonest cause of a slow tree.
//
//   <SessionContext.Provider value={{ userId, signOut }}>

export function SessionProvider({ children }: { children: React.ReactNode }) {
  const [userId, setUserId] = useState('u1');

  const signOut = useCallback(() => setUserId(''), []);

  // RIGHT: a stable identity, so consumers re-render only when it changes.
  const value = useMemo(() => ({ userId, signOut }), [userId, signOut]);

  return <SessionContext.Provider value={value}>{children}</SessionContext.Provider>;
}

// Memoised, with a stable callback, so a parent render does not rebuild it.
const Row = memo(function Row({ label, onPress }: { label: string; onPress: () => void }) {
  // Temporary, while profiling - the Profiler names the prop that changed.
  if (__DEV__) console.log('render', label);

  return (
    <Pressable onPress={onPress}>
      <Text>{label}</Text>
    </Pressable>
  );
});

export function useSession() {
  const session = useContext(SessionContext);
  if (!session) throw new Error('useSession must be inside a SessionProvider');
  return session;
}

Open this example in the editor and change it. Nothing you type is saved.