From 798f2e47aafa555a99d596cefa6bf6972e17d52e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Wed, 26 Feb 2025 13:22:00 -0800 Subject: [PATCH] Remove unnecessary extra prototype from ReactNativeElement (#49699) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/49699 Changelog: [internal] Just a minor optimization in `ReactNativeElement`, to stop creating an unnecessary object in the prototype chain for the `super()` removal optimization. Reviewed By: huntie Differential Revision: D70250804 fbshipit-source-id: 1f8104f8e17f12264326cd715e07877a371f9dc5 --- .../__snapshots__/public-api-test.js.snap | 4 +- .../webapis/dom/nodes/ReactNativeElement.js | 54 +++++++++++-------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap index 597e90f992d..21cc69858c1 100644 --- a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap +++ b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap @@ -9238,7 +9238,7 @@ declare export function createReactNativeDocument( `; exports[`public API should not change unintentionally src/private/webapis/dom/nodes/ReactNativeElement.js 1`] = ` -"declare class ReactNativeElementMethods +"declare class ReactNativeElement extends ReadOnlyElement implements LegacyHostInstanceMethods { @@ -9264,7 +9264,7 @@ exports[`public API should not change unintentionally src/private/webapis/dom/no ): void; setNativeProps(nativeProps: { ... }): void; } -declare export default typeof ReactNativeElementMethods; +declare export default typeof ReactNativeElement; " `; diff --git a/packages/react-native/src/private/webapis/dom/nodes/ReactNativeElement.js b/packages/react-native/src/private/webapis/dom/nodes/ReactNativeElement.js index 288ac422c25..362726e0b17 100644 --- a/packages/react-native/src/private/webapis/dom/nodes/ReactNativeElement.js +++ b/packages/react-native/src/private/webapis/dom/nodes/ReactNativeElement.js @@ -60,7 +60,7 @@ const noop = () => {}; // was slower than this method because the engine has to create an object than // we then discard to create a new one. -class ReactNativeElementMethods +class ReactNativeElement extends ReadOnlyElement implements LegacyHostInstanceMethods { @@ -227,28 +227,36 @@ class ReactNativeElementMethods } } -// Alternative constructor just implemented to provide a better performance than -// calling super() in the original class. -function ReactNativeElement( - this: ReactNativeElementMethods, - tag: number, - viewConfig: ViewConfig, - internalInstanceHandle: InternalInstanceHandle, - ownerDocument: ReactNativeDocument, -) { - // Inlined from `ReadOnlyNode` - setOwnerDocument(this, ownerDocument); - setInstanceHandle(this, internalInstanceHandle); +type ReactNativeElementT = ReactNativeElement; - this.__nativeTag = tag; - this.__internalInstanceHandle = internalInstanceHandle; - this.__viewConfig = viewConfig; +function replaceConstructorWithoutSuper( + ReactNativeElementClass: Class, +): Class { + // Alternative constructor just implemented to provide a better performance than + // calling super() in the original class. + // eslint-disable-next-line no-shadow + function ReactNativeElement( + this: ReactNativeElementT, + tag: number, + viewConfig: ViewConfig, + internalInstanceHandle: InternalInstanceHandle, + ownerDocument: ReactNativeDocument, + ) { + // Inlined from `ReadOnlyNode` + setOwnerDocument(this, ownerDocument); + setInstanceHandle(this, internalInstanceHandle); + + this.__nativeTag = tag; + this.__internalInstanceHandle = internalInstanceHandle; + this.__viewConfig = viewConfig; + } + + ReactNativeElement.prototype = ReactNativeElementClass.prototype; + + // $FlowExpectedError[incompatible-return] + return ReactNativeElement; } -ReactNativeElement.prototype = Object.create( - ReactNativeElementMethods.prototype, -); - -// $FlowExpectedError[prop-missing] -// $FlowFixMe[incompatible-cast] -export default ReactNativeElement as typeof ReactNativeElementMethods; +export default replaceConstructorWithoutSuper( + ReactNativeElement, +) as typeof ReactNativeElement;