Skip to content

Commit fee1a09

Browse files
authored
[iOS] Avoid unnecessary string allocations (#3499)
## Description #3290 introduced recursive algorithm to obtain accessibility label from children. However, unlike in React Native, it allocates `String` at every recursive call. This may lower the performance as those allocations come at high cost. ## Test plan Accessibility labels are inferred as before.
1 parent b10c263 commit fee1a09

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

apple/RNGestureHandlerButton.mm

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,24 @@ - (NSString *)accessibilityLabel
135135
// Vendored from RCTView.m to infer accessibility label from children
136136
static NSString *RNGHRecursiveAccessibilityLabel(UIView *view)
137137
{
138-
NSMutableString *str = [NSMutableString stringWithString:@""];
138+
NSMutableString *str = nil;
139139
for (UIView *subview in view.subviews) {
140140
NSString *label = subview.accessibilityLabel;
141141
if (!label) {
142142
label = RNGHRecursiveAccessibilityLabel(subview);
143143
}
144144
if (label && label.length > 0) {
145+
if (str == nil) {
146+
str = [NSMutableString string];
147+
}
145148
if (str.length > 0) {
146149
[str appendString:@" "];
147150
}
148151
[str appendString:label];
149152
}
150153
}
151-
return str.length == 0 ? nil : str;
154+
155+
return str;
152156
}
153157
#endif
154158

0 commit comments

Comments
 (0)