Skip to content

Commit

Permalink
chore: do several perf improvements (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Jul 6, 2022
1 parent 99925a0 commit 31ac323
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-kiwis-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Optimize perf by using smarter string builder
33 changes: 14 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,10 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
if (Array.isArray(vnode)) {
let rendered = '';
for (let i = 0; i < vnode.length; i++) {
if (pretty && i > 0) rendered += '\n';
rendered += _renderToString(
vnode[i],
context,
opts,
inner,
isSvgMode,
selectValue
);
if (pretty && i > 0) rendered = rendered + '\n';
rendered =
rendered +
_renderToString(vnode[i], context, opts, inner, isSvgMode, selectValue);
}
return rendered;
}
Expand Down Expand Up @@ -289,7 +284,7 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
opts.attributeHook &&
opts.attributeHook(name, v, context, opts, isComponent);
if (hooked || hooked === '') {
s += hooked;
s = s + hooked;
continue;
}

Expand All @@ -303,7 +298,7 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
v = name;
// in non-xml mode, allow boolean attributes
if (!opts || !opts.xml) {
s += ' ' + name;
s = s + ' ' + name;
continue;
}
}
Expand All @@ -319,10 +314,10 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
// and the <option> doesn't already have a selected attribute on it
typeof props.selected === 'undefined'
) {
s += ` selected`;
s = s + ` selected`;
}
}
s += ` ${name}="${encodeEntities(v)}"`;
s = s + ` ${name}="${encodeEntities(v)}"`;
}
}
}
Expand All @@ -331,10 +326,10 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
if (pretty) {
let sub = s.replace(/\n\s*/, ' ');
if (sub !== s && !~sub.indexOf('\n')) s = sub;
else if (pretty && ~s.indexOf('\n')) s += '\n';
else if (pretty && ~s.indexOf('\n')) s = s + '\n';
}

s += '>';
s = s + '>';

if (UNSAFE_NAME.test(nodeName))
throw new Error(`${nodeName} is not a valid HTML tag name in ${s}`);
Expand All @@ -350,7 +345,7 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
if (pretty && isLargeString(html)) {
html = '\n' + indentChar + indent(html, indentChar);
}
s += html;
s = s + html;
} else if (
propChildren != null &&
getChildren((children = []), propChildren).length
Expand Down Expand Up @@ -407,16 +402,16 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
}

if (pieces.length || html) {
s += pieces.join('');
s = s + pieces.join('');
} else if (opts && opts.xml) {
return s.substring(0, s.length - 1) + ' />';
}

if (isVoid && !children && !html) {
s = s.replace(/>$/, ' />');
} else {
if (pretty && ~s.indexOf('\n')) s += '\n';
s += `</${nodeName}>`;
if (pretty && ~s.indexOf('\n')) s = s + '\n';
s = s + `</${nodeName}>`;
}

return s;
Expand Down

0 comments on commit 31ac323

Please sign in to comment.