From 38fefb2771d7ff65568fa989f2ca4a5bf44e58ec Mon Sep 17 00:00:00 2001 From: Sanjaiyan Parthipan Date: Tue, 15 Apr 2025 09:17:02 -0700 Subject: [PATCH] =?UTF-8?q?Minor=20Performance=20and=20Code=20Quality=20Im?= =?UTF-8?q?provements=20=E2=9C=88=EF=B8=8F=20(#50682)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: *Hi,* I made a small update to the dependency array in `useLayoutEffect`, changing it from `[native]` to `[native.value]` for better precision. Since JavaScript compares objects by reference, this change can lead to a minor performance improvement. Additionally, as `useLayoutEffect` is render-blocking, I wanted to ensure we optimize its usage as much as possible. As a micro-optimization and in line with good coding practices, I also changed a `let + if` variable to `const`. While the performance gain is minimal, it contributes to cleaner and more consistent code. Please feel free to review, and I sincerely apologize if I made any mistakes in the process. ## Changelog: *[General] [Changed]* – Refined `useLayoutEffect` dependency array from `[native]` to `[native.value]` for improved precision and efficiency in re-renders. *[General] [Changed]* – Replaced `let` with `const` where applicable for better code standards and micro-optimization. Pull Request resolved: https://github.com/facebook/react-native/pull/50682 Reviewed By: huntie Differential Revision: D72979663 Pulled By: yungsters fbshipit-source-id: 64ac09811b78ca67be903d8cd91da8cd6f0a45fa --- .../Libraries/Components/Switch/Switch.js | 2 +- .../Libraries/Components/View/View.js | 39 +++++++++---------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/packages/react-native/Libraries/Components/Switch/Switch.js b/packages/react-native/Libraries/Components/Switch/Switch.js index cf00162a14f..057b8fcb155 100644 --- a/packages/react-native/Libraries/Components/Switch/Switch.js +++ b/packages/react-native/Libraries/Components/Switch/Switch.js @@ -213,7 +213,7 @@ const Switch: component( SwitchCommands.setValue(nativeSwitchRef.current, jsValue); } } - }, [value, native]); + }, [value, native.value]); if (Platform.OS === 'android') { const {onTintColor, tintColor, ...androidProps} = restProps; diff --git a/packages/react-native/Libraries/Components/View/View.js b/packages/react-native/Libraries/Components/View/View.js index 4f13e1ec49d..5e1cb146902 100644 --- a/packages/react-native/Libraries/Components/View/View.js +++ b/packages/react-native/Libraries/Components/View/View.js @@ -61,38 +61,35 @@ const View: component( const _accessibilityLabelledBy = ariaLabelledBy?.split(/\s*,\s*/g) ?? accessibilityLabelledBy; - let _accessibilityState; - if ( + const _accessibilityState = accessibilityState != null || ariaBusy != null || ariaChecked != null || ariaDisabled != null || ariaExpanded != null || ariaSelected != null - ) { - _accessibilityState = { - busy: ariaBusy ?? accessibilityState?.busy, - checked: ariaChecked ?? accessibilityState?.checked, - disabled: ariaDisabled ?? accessibilityState?.disabled, - expanded: ariaExpanded ?? accessibilityState?.expanded, - selected: ariaSelected ?? accessibilityState?.selected, - }; - } - let _accessibilityValue; - if ( + ? { + busy: ariaBusy ?? accessibilityState?.busy, + checked: ariaChecked ?? accessibilityState?.checked, + disabled: ariaDisabled ?? accessibilityState?.disabled, + expanded: ariaExpanded ?? accessibilityState?.expanded, + selected: ariaSelected ?? accessibilityState?.selected, + } + : undefined; + + const _accessibilityValue = accessibilityValue != null || ariaValueMax != null || ariaValueMin != null || ariaValueNow != null || ariaValueText != null - ) { - _accessibilityValue = { - max: ariaValueMax ?? accessibilityValue?.max, - min: ariaValueMin ?? accessibilityValue?.min, - now: ariaValueNow ?? accessibilityValue?.now, - text: ariaValueText ?? accessibilityValue?.text, - }; - } + ? { + max: ariaValueMax ?? accessibilityValue?.max, + min: ariaValueMin ?? accessibilityValue?.min, + now: ariaValueNow ?? accessibilityValue?.now, + text: ariaValueText ?? accessibilityValue?.text, + } + : undefined; const actualView = (