Skip to content

Commit

Permalink
Optimise perf (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Aug 7, 2024
1 parent 883e02b commit 45b8e8b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-apes-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Improve perf a bit by hoisting the typeof check to reduce calling typeof
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"@babel/register": "^7.12.10",
"@changesets/changelog-github": "^0.4.1",
"@changesets/cli": "^2.18.0",
"baseline-rts": "npm:preact-render-to-string@6.5.7",
"baseline-rts": "npm:preact-render-to-string@latest",
"benchmarkjs-pretty": "^2.0.1",
"chai": "^4.2.0",
"check-export-map": "^1.3.1",
Expand Down
21 changes: 11 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,11 @@ function _renderToString(
return EMPTY_STR;
}

let vnodeType = typeof vnode;
// Text VNodes: escape as HTML
if (typeof vnode !== 'object') {
if (typeof vnode === 'function') return EMPTY_STR;
return typeof vnode === 'string'
? encodeEntities(vnode)
: vnode + EMPTY_STR;
if (vnodeType !== 'object') {
if (vnodeType === 'function') return EMPTY_STR;
return vnodeType === 'string' ? encodeEntities(vnode) : vnode + EMPTY_STR;
}

// Recurse into children / Arrays
Expand Down Expand Up @@ -305,7 +304,7 @@ function _renderToString(
if (typeof type === 'function') {
if (type === Fragment) {
// Serialized precompiled JSX.
if (props.tpl) {
if ('tpl' in props) {
let out = EMPTY_STR;
for (let i = 0; i < props.tpl.length; i++) {
out = out + props.tpl[i];
Expand Down Expand Up @@ -338,7 +337,7 @@ function _renderToString(
}

return out;
} else if (props.UNSTABLE_comment) {
} else if ('UNSTABLE_comment' in props) {
// Fragments are the least used components of core that's why
// branching here for comments has the least effect on perf.
return (
Expand Down Expand Up @@ -402,7 +401,8 @@ function _renderToString(
let isTopLevelFragment =
rendered != null &&
rendered.type === Fragment &&
rendered.key == null;
rendered.key == null &&
!('tpl' in rendered.props);
rendered = isTopLevelFragment ? rendered.props.children : rendered;

try {
Expand Down Expand Up @@ -437,7 +437,8 @@ function _renderToString(
let isTopLevelFragment =
rendered != null &&
rendered.type === Fragment &&
rendered.key == null;
rendered.key == null &&
!('tpl' in rendered.props);
rendered = isTopLevelFragment ? rendered.props.children : rendered;

str = _renderToString(
Expand Down Expand Up @@ -467,7 +468,7 @@ function _renderToString(
rendered != null &&
rendered.type === Fragment &&
rendered.key == null &&
rendered.props.tpl == null;
!('tpl' in rendered.props);
rendered = isTopLevelFragment ? rendered.props.children : rendered;

try {
Expand Down

0 comments on commit 45b8e8b

Please sign in to comment.