Skip to content

[iOS] Consider caching accessibilityLabel for RNGestureHandlerButton #4701

Open

Description

@gigobyte

Description

While testing performance with iOS accessibility settings turned on (Voice Control), we found that the app becomes extremely laggy: scrolling and opening screens freeze for several seconds.

This comes from how Voice Control queries the accessibility tree.

  1. After new content mounts, it asks the app many times per second which element is focused. UIKit answers each request synchronously on the main thread by walking the whole view hierarchy. During that walk, React Native's Text (RCTParagraphComponentView) runs isAccessibilityCoopted, which walks up through every ancestor of every Text and reads accessibilityLabel on any non-RN view, only to check that a label exists.
  2. When the ancestor is an RNGH button with no explicit label, its getter (RNGHRecursiveAccessibilityLabel) rebuilds the label from the button's whole subtree, creating a new NSAttributedString for every Text inside it. A button with N texts therefore rebuilds its label N + 1 times per query, and this repeats for every mounted button on every query.

In our profiles (Time Profiler, iPhone 17 Pro, iOS 26.6.1), this accounted for ~3.3s of main-thread time plus ~2.3s spent building attributed strings on a busy screen.

React Native already knows this lookup is expensive and avoids it for its own views (facebook/react-native#50265):

} else if (![ancestor isKindOfClass:[RCTViewComponentView class]] && ancestor.accessibilityLabel) {
  // Same as above, for UIView case. Cannot call this on RCTViewComponentView
  // as it is recursive and quite expensive.
  return NO;
}

RNGH can't benefit from this. RNGestureHandlerButtonComponentView is an RCTViewComponentView, but children are mounted inside the inner RNGestureHandlerButton, which is a UIControl. React Native reaches that control first and calls its expensive getter.

Therefore, we propose caching the inferred label for the current main-queue turn. An accessibility query runs synchronously on the main thread and the view tree can't change during it, so the label is built once per query instead of once per Text, and it is never stale. With this change, the time spent in the getter dropped by 82% (3,285ms → 593ms) and attributed-string building by 80% (2,259ms → 457ms):

 @implementation RNGestureHandlerButton {
   ...
+  BOOL _isRecursiveAccessibilityLabelCached;
+  NSString *_cachedRecursiveAccessibilityLabel;
 }

 - (NSString *)accessibilityLabel
 {
   NSString *label = super.accessibilityLabel;
   if (label) {
     return label;
   }
-  return RNGHRecursiveAccessibilityLabel(self);
+  if (!_isRecursiveAccessibilityLabelCached) {
+    _cachedRecursiveAccessibilityLabel = RNGHRecursiveAccessibilityLabel(self);
+    _isRecursiveAccessibilityLabelCached = YES;
+    __weak RNGestureHandlerButton *weakSelf = self;
+    dispatch_async(dispatch_get_main_queue(), ^{
+      RNGestureHandlerButton *strongSelf = weakSelf;
+      if (strongSelf != nil) {
+        strongSelf->_isRecursiveAccessibilityLabelCached = NO;
+        strongSelf->_cachedRecursiveAccessibilityLabel = nil;
+      }
+    });
+  }
+  return _cachedRecursiveAccessibilityLabel;
 }

Steps to reproduce

  1. On a physical iPhone, enable Voice Control (Settings → Accessibility → Voice Control). Simulators don't have this setting.
  2. Run the snippet below and scroll up and down.
  3. Scrolling stutters while new rows mount.
  4. For comparison, disable Voice Control and repeat: scrolling is smooth.
import { FlatList, Text, View } from 'react-native';
import { GestureHandlerRootView, Touchable } from 'react-native-gesture-handler';

const data = Array.from({ length: 80 }, (_, i) => i);

export default function App() {
  return (
    <GestureHandlerRootView style={{ flex: 1 }}>
      <FlatList
        data={data}
        keyExtractor={String}
        renderItem={({ item }) => (
          <Touchable onPress={() => {}} style={{ padding: 12 }}>
            <View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
              <View>
                <Text>Item {item}</Text>
                <Text>Subtitle {item}</Text>
              </View>
              <View>
                <Text>123.45</Text>
                <Text>+1.23%</Text>
              </View>
            </View>
          </Touchable>
        )}
      />
    </GestureHandlerRootView>
  );
}

A link to a Gist, an Expo Snack or a link to a repository based on this template that reproduces the bug.

Gesture Handler version

3.5.0

React Native version

0.86.3

Platforms

iOS

JavaScript runtime

None

Workflow

None

Build type

None

Device

Real device

Device model

No response

Acknowledgements

Yes

Activity

  1. added
    Platform: iOS
    on Nov 17, 2026
  2. github-actions commented on Nov 17, 2026

    @github-actions

    Hey @gigobyte! 👋

    Some of the required information in this issue is missing, incomplete or invalid. Could you take another look at the template and make sure each of these is filled in with something we can act on?

    • Description - what happens, and what you expected to happen instead
    • Steps to reproduce - steps somebody else can follow, not just numbering
    • Gesture Handler version - the exact version from your package.json or lockfile
    • React Native version - the exact version from your package.json or lockfile
    • Versions of related libraries you mention (Reanimated, Worklets, Screens, Expo) - the versions actually installed
    • Platforms - the platforms you actually see the bug on

    Once the issue is updated we will take another look. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Missing infoPlatform: iOS

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions