From 4280a9e57305b8b67dc2f6016f135cc41b4d1fa5 Mon Sep 17 00:00:00 2001 From: jdecroock Date: Wed, 20 Aug 2025 17:21:41 +0200 Subject: [PATCH 1/5] Save bytes on hydration 2.0 --- src/diff/index.js | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/diff/index.js b/src/diff/index.js index 7cffa6d7ea..b34c43890a 100644 --- a/src/diff/index.js +++ b/src/diff/index.js @@ -75,13 +75,13 @@ export function diff( if (newVNode.constructor != UNDEFINED) return NULL; // If the previous diff bailed out, resume creating/hydrating. - if (oldVNode._flags & MODE_SUSPENDED) { - isHydrating = !!(oldVNode._flags & MODE_HYDRATE); - if (oldVNode._component._excess) { - excessDomChildren = oldVNode._component._excess; - oldDom = newVNode._dom = oldVNode._dom = excessDomChildren[0]; - oldVNode._component._excess = null; - } + if ( + oldVNode._flags & MODE_SUSPENDED && + (isHydrating = !!(oldVNode._flags & MODE_HYDRATE)) + ) { + excessDomChildren = oldVNode._component._excess; + oldDom = newVNode._dom = oldVNode._dom = excessDomChildren[0]; + oldVNode._component._excess = NULL; } if ((tmp = options._diff)) tmp(newVNode); @@ -311,7 +311,7 @@ export function diff( if (isHydrating || excessDomChildren != NULL) { if (e.then) { let commentMarkersToFind = 0, - done = false; + done; newVNode._flags |= isHydrating ? MODE_HYDRATE | MODE_SUSPENDED @@ -329,20 +329,22 @@ export function diff( // We exclude the open and closing marker from // the future excessDomChildren but any nested one // needs to be included for future suspensions. - if (child.nodeType == 8 && child.data == '$s') { - if (commentMarkersToFind > 0) { - newVNode._component._excess.push(child); + if (child.nodeType == 8) { + if (child.data == '$s') { + if (commentMarkersToFind > 0) { + newVNode._component._excess.push(child); + } + commentMarkersToFind++; + excessDomChildren[i] = NULL; + } else if (child.data == '/$s') { + commentMarkersToFind--; + if (commentMarkersToFind > 0) { + newVNode._component._excess.push(child); + } + done = commentMarkersToFind == 0; + oldDom = excessDomChildren[i]; + excessDomChildren[i] = NULL; } - commentMarkersToFind++; - excessDomChildren[i] = NULL; - } else if (child.nodeType == 8 && child.data == '/$s') { - commentMarkersToFind--; - if (commentMarkersToFind > 0) { - newVNode._component._excess.push(child); - } - done = commentMarkersToFind === 0; - oldDom = excessDomChildren[i]; - excessDomChildren[i] = NULL; } else if (commentMarkersToFind > 0) { newVNode._component._excess.push(child); excessDomChildren[i] = NULL; From a728eef9072bd9080a43f191d579691ad5e9bb65 Mon Sep 17 00:00:00 2001 From: jdecroock Date: Wed, 20 Aug 2025 17:33:44 +0200 Subject: [PATCH 2/5] Check truthy instead --- src/diff/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/diff/index.js b/src/diff/index.js index b34c43890a..d00e299ca8 100644 --- a/src/diff/index.js +++ b/src/diff/index.js @@ -331,21 +331,21 @@ export function diff( // needs to be included for future suspensions. if (child.nodeType == 8) { if (child.data == '$s') { - if (commentMarkersToFind > 0) { + if (commentMarkersToFind) { newVNode._component._excess.push(child); } commentMarkersToFind++; excessDomChildren[i] = NULL; } else if (child.data == '/$s') { commentMarkersToFind--; - if (commentMarkersToFind > 0) { + if (commentMarkersToFind) { newVNode._component._excess.push(child); } done = commentMarkersToFind == 0; oldDom = excessDomChildren[i]; excessDomChildren[i] = NULL; } - } else if (commentMarkersToFind > 0) { + } else if (commentMarkersToFind) { newVNode._component._excess.push(child); excessDomChildren[i] = NULL; } From c2871146ffd78efce8041fb9e788eb2ca2d88634 Mon Sep 17 00:00:00 2001 From: jdecroock Date: Wed, 20 Aug 2025 17:38:46 +0200 Subject: [PATCH 3/5] We already assign to newvnode._dom in catch --- src/diff/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diff/index.js b/src/diff/index.js index d00e299ca8..3b099c8239 100644 --- a/src/diff/index.js +++ b/src/diff/index.js @@ -80,7 +80,7 @@ export function diff( (isHydrating = !!(oldVNode._flags & MODE_HYDRATE)) ) { excessDomChildren = oldVNode._component._excess; - oldDom = newVNode._dom = oldVNode._dom = excessDomChildren[0]; + oldDom = excessDomChildren[0]; oldVNode._component._excess = NULL; } From 83827a5a58fb6e611fc7fd59fe2f48ae13e0e539 Mon Sep 17 00:00:00 2001 From: jdecroock Date: Wed, 20 Aug 2025 17:45:31 +0200 Subject: [PATCH 4/5] Always remove comments from excessDomChildren --- src/diff/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/diff/index.js b/src/diff/index.js index 3b099c8239..b87d779222 100644 --- a/src/diff/index.js +++ b/src/diff/index.js @@ -335,7 +335,6 @@ export function diff( newVNode._component._excess.push(child); } commentMarkersToFind++; - excessDomChildren[i] = NULL; } else if (child.data == '/$s') { commentMarkersToFind--; if (commentMarkersToFind) { @@ -343,8 +342,8 @@ export function diff( } done = commentMarkersToFind == 0; oldDom = excessDomChildren[i]; - excessDomChildren[i] = NULL; } + excessDomChildren[i] = NULL; } else if (commentMarkersToFind) { newVNode._component._excess.push(child); excessDomChildren[i] = NULL; From 86dedc650d9c8d8f7f4c5c271ebbc33fa3567f5f Mon Sep 17 00:00:00 2001 From: jdecroock Date: Wed, 20 Aug 2025 17:57:53 +0200 Subject: [PATCH 5/5] Save 1 byte by not casting --- src/diff/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/diff/index.js b/src/diff/index.js index b87d779222..eab6f4c010 100644 --- a/src/diff/index.js +++ b/src/diff/index.js @@ -77,7 +77,8 @@ export function diff( // If the previous diff bailed out, resume creating/hydrating. if ( oldVNode._flags & MODE_SUSPENDED && - (isHydrating = !!(oldVNode._flags & MODE_HYDRATE)) + // @ts-expect-error This is 1 or 0 (true or false) + (isHydrating = oldVNode._flags & MODE_HYDRATE) ) { excessDomChildren = oldVNode._component._excess; oldDom = excessDomChildren[0];