Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimise perf with polymorphic props access and typeof repetition #385

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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