Skip to content

Commit

Permalink
improve perf with array pre-allocate
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Dec 12, 2024
1 parent eafe5a3 commit 7fcc557
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-schools-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Improve performance by pre-allocating arrays
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ function _renderToString(
let rendered = EMPTY_STR,
renderArray;
parent[CHILDREN] = vnode;
for (let i = 0; i < vnode.length; i++) {
const vnodeLength = vnode.length;
for (let i = 0; i < vnodeLength; i++) {
let child = vnode[i];
if (child == null || typeof child == 'boolean') continue;

Expand All @@ -262,7 +263,7 @@ function _renderToString(
rendered = rendered + childRender;
} else {
if (!renderArray) {
renderArray = [];
renderArray = new Array(vnodeLength);
}

if (rendered) renderArray.push(rendered);
Expand Down

0 comments on commit 7fcc557

Please sign in to comment.