A blocked JavaScript thread doesn't stop the whole app. The main thread draws native views and runs some interactions without any help from JavaScript, so a ScrollView keeps scrolling through a long render. The main thread can also miss frames on its own, and then the fix lives in the views you render and how you move them.

Key terms

  • Main thread: the native UI thread, where on Android the standard measure, layout and draw happen.
  • Render thread: on Android 5.0 and up, the thread that generates the OpenGL commands used to draw the UI.
  • renderToHardwareTextureAndroid: an Android-only View prop, false by default, that renders a View and its children into a single GPU hardware texture.
  • needsOffscreenAlphaCompositing: a View prop, false by default, that renders a view offscreen to preserve exact alpha behavior.

Native work that doesn't wait

A ScrollView lives on the main thread. Its scroll events are dispatched to the JavaScript thread, but their receipt is not needed for the scroll to happen, so you can keep scrolling while JavaScript is locked up. Native stack navigators behave the same way: their transition animations run on the native main UI thread and are not interrupted by frame drops on the JavaScript thread, which is why they perform better than JavaScript-based stack navigators.

Touches start on the main thread too, which sends the raw touch events over to the JavaScript thread. That split explains the mixed symptoms of a blocked JavaScript thread: a gesture the main thread handles alone keeps going, while anything that needs a JavaScript handler to respond waits.

On Android, a trace names the threads involved. The UI thread runs measure, layout and draw, the render thread produces the OpenGL commands, and JavaScript runs on a thread named mqt_js or left unnamed. When the UI and render threads have work crossing frame boundaries, the problem lies in the native views being rendered.

Moving views without dropping UI frames

Moving a view on screen, whether by scrolling, translating or rotating it, can drop UI thread FPS. Android is especially sensitive when text with a transparent background sits on top of an image, or in any other case where alpha compositing is needed to redraw the view on each frame. Enabling renderToHardwareTextureAndroid can help with this significantly.

With the prop on, the View and its children are rendered into one GPU hardware texture. For animations that only change opacity, rotation, translation or scale, the view doesn't have to be redrawn: the texture is reused and composited again with new parameters. The cost is video memory, which is limited, and overusing the prop can push memory use very high. Profile memory along with frame rate, and set the prop back to false once the view stops moving.

The SlidingCaption example turns the texture on only while its card moves:

tsx
<View
  renderToHardwareTextureAndroid={isMoving}
  style={[styles.card, {transform: [{translateX: offsetX}]}]}>

The photo is an absolutely positioned Image behind the caption text, the layout React Native 0.88 recommends in place of the deprecated ImageBackground. The parent that drives offsetX and isMoving is left out. The textured View is the one being translated, and the flag drops back to false when the slide ends.

Leave needsOffscreenAlphaCompositing off when the GPU is the bottleneck. It is false by default because rendering offscreen to preserve exact alpha behavior is extremely expensive, and enabling it greatly increases the GPU load of every frame.

The iOS counterpart, shouldRasterizeIOS, renders the View as a bitmap before compositing, which costs an offscreen drawing pass and memory. The performance guide claims it is already enabled by default, but its default is false, so don't assume iOS rasterizes your views for you.

No prop helps when the frames drop because new views are being constructed during an interaction. Postpone creating the new UI until the interaction is over, or simplify it.

Zooming an image with a scale transform

On iOS, every change to an Image component's width or height re-crops and re-scales it from the original image, which can be very expensive for large images. Growing a product thumbnail into a full-screen preview by animating width and height does that work on every step. Animate the size with the transform: [{scale}] style instead.

The ZoomablePhoto example keeps the image at a fixed 320 by 240 in its stylesheet and applies the zoom as {transform: [{scale: zoom}]}. Its width and height never change, however far the zoom goes.

Code Examples

tsx
import {Image, StyleSheet, Text, View} from 'react-native';

type SlidingCaptionProps = {
  uri: string;
  caption: string;
  offsetX: number;
  isMoving: boolean;
};

export function SlidingCaption({uri, caption, offsetX, isMoving}: SlidingCaptionProps) {
  return (
    <View
      renderToHardwareTextureAndroid={isMoving}
      style={[styles.card, {transform: [{translateX: offsetX}]}]}>
      <Image source={{uri}} style={styles.photo} />
      <Text style={styles.caption}>{caption}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  card: {width: 320, height: 240, justifyContent: 'flex-end'},
  photo: {position: 'absolute', top: 0, left: 0, width: 320, height: 240},
  caption: {color: 'white', padding: 8, backgroundColor: 'rgba(0, 0, 0, 0.4)'},
});
tsx
import {Image, StyleSheet} from 'react-native';

type ZoomablePhotoProps = {
  uri: string;
  zoom: number;
};

export function ZoomablePhoto({uri, zoom}: ZoomablePhotoProps) {
  return (
    <Image source={{uri}} style={[styles.photo, {transform: [{scale: zoom}]}]} />
  );
}

const styles = StyleSheet.create({
  photo: {width: 320, height: 240},
});
✓ Completed