From ea55d5e8ede4934faa68cc49cbfb432cf614fe31 Mon Sep 17 00:00:00 2001 From: myfreeer Date: Sat, 12 Sep 2020 12:32:19 +0800 Subject: [PATCH] parse-sax: use native TextDecoder in browser when available 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: https://github.com/feross/buffer/issues/268 https://github.com/feross/buffer/issues/60 https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder --- lib/utils/parse-sax.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/utils/parse-sax.js b/lib/utils/parse-sax.js index b9c830917..f32fc011a 100644 --- a/lib/utils/parse-sax.js +++ b/lib/utils/parse-sax.js @@ -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 @@ -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;