diff --git a/.changeset/perfect-hounds-joke.md b/.changeset/perfect-hounds-joke.md new file mode 100644 index 00000000..b987e728 --- /dev/null +++ b/.changeset/perfect-hounds-joke.md @@ -0,0 +1,5 @@ +--- +'preact-render-to-string': patch +--- + +Perf: Remove unnecessary closure when rendering child nodes diff --git a/src/index.js b/src/index.js index 903216c1..4c652a68 100644 --- a/src/index.js +++ b/src/index.js @@ -441,8 +441,9 @@ function _renderToString( rendered.props.tpl == null; rendered = isTopLevelFragment ? rendered.props.children : rendered; - const renderChildren = () => - _renderToString( + try { + // Recurse into children before invoking the after-diff hook + const str = _renderToString( rendered, context, isSvgMode, @@ -451,10 +452,6 @@ function _renderToString( asyncMode ); - try { - // Recurse into children before invoking the after-diff hook - const str = renderChildren(); - if (afterDiff) afterDiff(vnode); vnode[PARENT] = null; @@ -468,12 +465,27 @@ function _renderToString( const renderNestedChildren = () => { try { - return renderChildren(); + return _renderToString( + rendered, + context, + isSvgMode, + selectValue, + vnode, + asyncMode + ); } catch (e) { if (!e || typeof e.then !== 'function') throw e; return e.then( - () => renderChildren(), + () => + _renderToString( + rendered, + context, + isSvgMode, + selectValue, + vnode, + asyncMode + ), () => renderNestedChildren() ); }