Skip to content

Commit

Permalink
Merge pull request #348 from preactjs/remove-closure
Browse files Browse the repository at this point in the history
perf: remove unnecessary closure
  • Loading branch information
marvinhagemeister committed Apr 9, 2024
2 parents e9a53aa + 303b8c0 commit ee7bc5a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-hounds-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Perf: Remove unnecessary closure when rendering child nodes
28 changes: 20 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand All @@ -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()
);
}
Expand Down

0 comments on commit ee7bc5a

Please sign in to comment.