From 915dfe6977e17dfdf364bced50e7c26d54ee72ab Mon Sep 17 00:00:00 2001 From: Benedikt Meurer Date: Thu, 27 Jun 2019 16:23:14 +0200 Subject: [PATCH] Slightly improve performance of hydration. (#15998) * Slightly improve performance of hydration. Avoid loading nodeType and data couple times from the same node in a row, but instead load them only once, which will help engines to run this code faster, especially during startup of the application. The general approach is still not ideal, since hydrating this way forces the browser engine to materialize JavaScript wrapper objects for all DOM nodes, even if they are not interesting to hydration itself. * Fix condition for COMMENT_NODEs. * Improve general code readability --- .../src/client/ReactDOMHostConfig.js | 56 +++++++++---------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/packages/react-dom/src/client/ReactDOMHostConfig.js b/packages/react-dom/src/client/ReactDOMHostConfig.js index ad51fdaae4..81c5f2fd92 100644 --- a/packages/react-dom/src/client/ReactDOMHostConfig.js +++ b/packages/react-dom/src/client/ReactDOMHostConfig.js @@ -670,44 +670,40 @@ export function registerSuspenseInstanceRetry( instance._reactRetry = callback; } +function getNextHydratable(node) { + // Skip non-hydratable nodes. + for (; node != null; node = node.nextSibling) { + const nodeType = node.nodeType; + if (nodeType === ELEMENT_NODE || nodeType === TEXT_NODE) { + break; + } + if (enableSuspenseServerRenderer) { + if (nodeType === COMMENT_NODE) { + break; + } + const nodeData = (node: any).data; + if ( + nodeData === SUSPENSE_START_DATA || + nodeData === SUSPENSE_FALLBACK_START_DATA || + nodeData === SUSPENSE_PENDING_START_DATA + ) { + break; + } + } + } + return (node: any); +} + export function getNextHydratableSibling( instance: HydratableInstance, ): null | HydratableInstance { - let node = instance.nextSibling; - // Skip non-hydratable nodes. - while ( - node && - node.nodeType !== ELEMENT_NODE && - node.nodeType !== TEXT_NODE && - (!enableSuspenseServerRenderer || - node.nodeType !== COMMENT_NODE || - ((node: any).data !== SUSPENSE_START_DATA && - (node: any).data !== SUSPENSE_PENDING_START_DATA && - (node: any).data !== SUSPENSE_FALLBACK_START_DATA)) - ) { - node = node.nextSibling; - } - return (node: any); + return getNextHydratable(instance.nextSibling); } export function getFirstHydratableChild( parentInstance: Container | Instance, ): null | HydratableInstance { - let next = parentInstance.firstChild; - // Skip non-hydratable nodes. - while ( - next && - next.nodeType !== ELEMENT_NODE && - next.nodeType !== TEXT_NODE && - (!enableSuspenseServerRenderer || - next.nodeType !== COMMENT_NODE || - ((next: any).data !== SUSPENSE_START_DATA && - (next: any).data !== SUSPENSE_FALLBACK_START_DATA && - (next: any).data !== SUSPENSE_PENDING_START_DATA)) - ) { - next = next.nextSibling; - } - return (next: any); + return getNextHydratable(parentInstance.firstChild); } export function hydrateInstance(