Skip to content

Commit 45b8e8b

Browse files
authored
Optimise perf (#385)
1 parent 883e02b commit 45b8e8b

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

Diff for: .changeset/proud-apes-tickle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'preact-render-to-string': patch
3+
---
4+
5+
Improve perf a bit by hoisting the typeof check to reduce calling typeof

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
"@babel/register": "^7.12.10",
133133
"@changesets/changelog-github": "^0.4.1",
134134
"@changesets/cli": "^2.18.0",
135-
"baseline-rts": "npm:preact-render-to-string@6.5.7",
135+
"baseline-rts": "npm:preact-render-to-string@latest",
136136
"benchmarkjs-pretty": "^2.0.1",
137137
"chai": "^4.2.0",
138138
"check-export-map": "^1.3.1",

Diff for: src/index.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,11 @@ function _renderToString(
236236
return EMPTY_STR;
237237
}
238238

239+
let vnodeType = typeof vnode;
239240
// Text VNodes: escape as HTML
240-
if (typeof vnode !== 'object') {
241-
if (typeof vnode === 'function') return EMPTY_STR;
242-
return typeof vnode === 'string'
243-
? encodeEntities(vnode)
244-
: vnode + EMPTY_STR;
241+
if (vnodeType !== 'object') {
242+
if (vnodeType === 'function') return EMPTY_STR;
243+
return vnodeType === 'string' ? encodeEntities(vnode) : vnode + EMPTY_STR;
245244
}
246245

247246
// Recurse into children / Arrays
@@ -305,7 +304,7 @@ function _renderToString(
305304
if (typeof type === 'function') {
306305
if (type === Fragment) {
307306
// Serialized precompiled JSX.
308-
if (props.tpl) {
307+
if ('tpl' in props) {
309308
let out = EMPTY_STR;
310309
for (let i = 0; i < props.tpl.length; i++) {
311310
out = out + props.tpl[i];
@@ -338,7 +337,7 @@ function _renderToString(
338337
}
339338

340339
return out;
341-
} else if (props.UNSTABLE_comment) {
340+
} else if ('UNSTABLE_comment' in props) {
342341
// Fragments are the least used components of core that's why
343342
// branching here for comments has the least effect on perf.
344343
return (
@@ -402,7 +401,8 @@ function _renderToString(
402401
let isTopLevelFragment =
403402
rendered != null &&
404403
rendered.type === Fragment &&
405-
rendered.key == null;
404+
rendered.key == null &&
405+
!('tpl' in rendered.props);
406406
rendered = isTopLevelFragment ? rendered.props.children : rendered;
407407

408408
try {
@@ -437,7 +437,8 @@ function _renderToString(
437437
let isTopLevelFragment =
438438
rendered != null &&
439439
rendered.type === Fragment &&
440-
rendered.key == null;
440+
rendered.key == null &&
441+
!('tpl' in rendered.props);
441442
rendered = isTopLevelFragment ? rendered.props.children : rendered;
442443

443444
str = _renderToString(
@@ -467,7 +468,7 @@ function _renderToString(
467468
rendered != null &&
468469
rendered.type === Fragment &&
469470
rendered.key == null &&
470-
rendered.props.tpl == null;
471+
!('tpl' in rendered.props);
471472
rendered = isTopLevelFragment ? rendered.props.children : rendered;
472473

473474
try {

0 commit comments

Comments
 (0)