Skip to content

Commit

Permalink
Merge pull request #228 from preactjs/perf-entity-encoding-scan
Browse files Browse the repository at this point in the history
[perf] Improve string encoding performance by ~50%
  • Loading branch information
marvinhagemeister authored Jul 21, 2022
2 parents 13b68cf + e4fe799 commit d7f599d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-poems-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"preact-render-to-string": patch
---

Improve string encoding performance by ~50%
36 changes: 27 additions & 9 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,34 @@ export const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine

const ENCODED_ENTITIES = /[&<>"]/;

export function encodeEntities(input) {
const s = String(input);
if (!ENCODED_ENTITIES.test(s)) {
return s;
export function encodeEntities(str) {
// Ensure we're always parsing and returning a string:
str += '';

// Skip all work for strings with no entities needing encoding:
if (ENCODED_ENTITIES.test(str) === false) return str;

let last = 0,
i = 0,
out = '',
ch = '';

// Seek forward in str until the next entity char:
for (; i<str.length; i++) {
switch (str.charCodeAt(i)) {
case 60: ch = '&lt;'; break;
case 62: ch = '&gt;'; break;
case 34: ch = '&quot;'; break;
case 38: ch = '&amp;'; break;
default: continue;
}
// Append skipped/buffered characters and the encoded entity:
if (i > last) out += str.slice(last, i);
out += ch;
// Start the next seek/buffer after the entity's offset:
last = i + 1;
}
return s
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
return out + str.slice(last, i);
}

export let indent = (s, char) =>
Expand Down

0 comments on commit d7f599d

Please sign in to comment.