Skip to content

Commit

Permalink
refactor(decode): Defer creating string to the end of the trie
Browse files Browse the repository at this point in the history
This should speed up legacy entities terminated by `;`.
  • Loading branch information
fb55 committed Dec 14, 2021
1 parent dde82e4 commit 0fa1149
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function getDecoder(decodeTree: Uint16Array) {
continue;
}

let result: string | null = null;
let resultIdx = 0;
let excess = 1;
let treeIdx = 0;
let current = decodeTree[treeIdx];
Expand All @@ -101,20 +101,23 @@ function getDecoder(decodeTree: Uint16Array) {
treeIdx += 1;
} else {
// If this is a surrogate pair, combine the higher bits from the node with the next byte
result =
current & BinTrieFlags.MULTI_BYTE
? String.fromCharCode(
decodeTree[++treeIdx],
decodeTree[++treeIdx]
)
: String.fromCharCode(decodeTree[++treeIdx]);
resultIdx = treeIdx;
treeIdx +=
1 +
Number((current & BinTrieFlags.MULTI_BYTE) !== 0);
excess = 0;
}
}
}

if (result != null) {
ret += result;
if (resultIdx !== 0) {
ret +=
decodeTree[resultIdx] & BinTrieFlags.MULTI_BYTE
? String.fromCharCode(
decodeTree[resultIdx + 1],
decodeTree[resultIdx + 2]
)
: String.fromCharCode(decodeTree[resultIdx + 1]);
lastIdx = strIdx - excess + 1;
}
}
Expand Down

0 comments on commit 0fa1149

Please sign in to comment.