summaryrefslogtreecommitdiff
path: root/ext/web/08_text_encoding.js
diff options
context:
space:
mode:
authorMarcos Casagrande <marcoscvp90@gmail.com>2022-10-23 21:09:15 +0200
committerGitHub <noreply@github.com>2022-10-23 21:09:15 +0200
commit0e1167d12d116be7ba1855252b5ca4d668bf3472 (patch)
tree9b23f8b6b0c5f55be6ff2e9d858a9ec81f1a49f7 /ext/web/08_text_encoding.js
parent45ac6e602da9cd016d4a3b093fd4873b90897f9a (diff)
perf(ext/web/encoding): avoid copy in decode (#16364)
Diffstat (limited to 'ext/web/08_text_encoding.js')
-rw-r--r--ext/web/08_text_encoding.js17
1 files changed, 6 insertions, 11 deletions
diff --git a/ext/web/08_text_encoding.js b/ext/web/08_text_encoding.js
index db724033f..4477d9b9e 100644
--- a/ext/web/08_text_encoding.js
+++ b/ext/web/08_text_encoding.js
@@ -23,7 +23,6 @@
StringPrototypeCharCodeAt,
StringPrototypeSlice,
TypedArrayPrototypeSubarray,
- TypedArrayPrototypeSlice,
Uint8Array,
Uint32Array,
} = window.__bootstrap.primordials;
@@ -404,27 +403,23 @@
*/
function decode(bytes, encoding) {
const BOMEncoding = BOMSniff(bytes);
- let start = 0;
if (BOMEncoding !== null) {
encoding = BOMEncoding;
- if (BOMEncoding === "UTF-8") start = 3;
- else start = 2;
+ const start = BOMEncoding === "UTF-8" ? 3 : 2;
+ bytes = TypedArrayPrototypeSubarray(bytes, start);
}
- return new TextDecoder(encoding).decode(
- TypedArrayPrototypeSlice(bytes, start),
- );
+ return new TextDecoder(encoding).decode(bytes);
}
/**
* @param {Uint8Array} bytes
*/
function BOMSniff(bytes) {
- const BOM = TypedArrayPrototypeSubarray(bytes, 0, 3);
- if (BOM[0] === 0xEF && BOM[1] === 0xBB && BOM[2] === 0xBF) {
+ if (bytes[0] === 0xEF && bytes[1] === 0xBB && bytes[2] === 0xBF) {
return "UTF-8";
}
- if (BOM[0] === 0xFE && BOM[1] === 0xFF) return "UTF-16BE";
- if (BOM[0] === 0xFF && BOM[1] === 0xFE) return "UTF-16LE";
+ if (bytes[0] === 0xFE && bytes[1] === 0xFF) return "UTF-16BE";
+ if (bytes[0] === 0xFF && bytes[1] === 0xFE) return "UTF-16LE";
return null;
}