Skip to content

Commit

Permalink
parse-sax: use native TextDecoder in browser when available
Browse files Browse the repository at this point in the history
Doing a profiling in chrome browser shows that the `Buffer.toString()` is using unexpected long cpu time. With the native TextDecoder it can get much faster in browsers supporting it.
In browsers not supporting TextDecoder, like Internet Explorer, we can fallback to `Buffer.toString()`.

References:
feross/buffer#268
feross/buffer#60
https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
  • Loading branch information
myfreeer committed Sep 12, 2020
1 parent 65ea2e4 commit ea55d5e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/utils/parse-sax.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
const {SaxesParser} = require('saxes');
const {PassThrough} = require('readable-stream');

// eslint-disable-next-line node/no-unsupported-features/node-builtins
const textDecoder = typeof TextDecoder === 'undefined' ? null : new TextDecoder('utf-8');
function toUtf8String(chunk) {
if (textDecoder) {
return textDecoder.decode(chunk);
}
return chunk.toString();
}

module.exports = async function*(iterable) {
// TODO: Remove once node v8 is deprecated
// Detect and upgrade old streams
Expand All @@ -17,7 +26,7 @@ module.exports = async function*(iterable) {
saxesParser.on('text', value => events.push({eventType: 'text', value}));
saxesParser.on('closetag', value => events.push({eventType: 'closetag', value}));
for await (const chunk of iterable) {
saxesParser.write(chunk.toString());
saxesParser.write(toUtf8String(chunk));
// saxesParser.write and saxesParser.on() are synchronous,
// so we can only reach the below line once all events have been emitted
if (error) throw error;
Expand Down

0 comments on commit ea55d5e

Please sign in to comment.