Skip to content

Commit 4a91b6f

Browse files
committed
benchmark: denoise TextDecoder benchmark a bit, add utf-16le/be
1 parent ea1e2a1 commit 4a91b6f

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

benchmark/util/text-decoder-stream.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
const common = require('../common.js');
44

5+
const MAX_N = 256 * 1024;
6+
const TOTAL_SIZE = 256 * 1024 * 1024;
7+
58
const bench = common.createBenchmark(main, {
69
encoding: ['utf-8', 'utf-16le'],
710
ignoreBOM: [0, 1],
811
fatal: [0, 1],
912
unicode: [0, 1],
1013
len: [256, 1024 * 16, 1024 * 128],
1114
chunks: [10],
12-
n: [1e3],
1315
type: ['SharedArrayBuffer', 'ArrayBuffer', 'Buffer'],
1416
});
1517

1618
const UNICODE_ALPHA = 'Blåbærsyltetøy';
1719
const ASCII_ALPHA = 'Blueberry jam';
1820

19-
function main({ encoding, len, unicode, chunks, n, ignoreBOM, type, fatal }) {
21+
function main({ encoding, len, unicode, chunks, ignoreBOM, type, fatal }) {
2022
const decoder = new TextDecoder(encoding, { ignoreBOM, fatal });
2123
let buf;
2224

@@ -41,6 +43,7 @@ function main({ encoding, len, unicode, chunks, n, ignoreBOM, type, fatal }) {
4143

4244
const chunk = Math.ceil(len / chunks);
4345
const max = len - chunk;
46+
const n = Math.min(MAX_N, Math.ceil(TOTAL_SIZE / len));
4447
bench.start();
4548
for (let i = 0; i < n; i++) {
4649
let pos = 0;

benchmark/util/text-decoder.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const common = require('../common.js');
44

55
const bench = common.createBenchmark(main, {
6-
encoding: ['utf-8', 'windows-1252', 'iso-8859-3'],
6+
encoding: ['utf-8', 'utf-16le', 'utf-16be', 'windows-1252', 'iso-8859-3'],
77
ignoreBOM: [0, 1],
88
fatal: [0, 1],
99
len: [256, 1024 * 16, 1024 * 128],
@@ -25,7 +25,7 @@ function main({ encoding, len, n, ignoreBOM, type, fatal }) {
2525
break;
2626
}
2727
case 'Buffer': {
28-
buf = Buffer.allocUnsafe(len);
28+
buf = Buffer.alloc(len);
2929
break;
3030
}
3131
}

lib/internal/encoding.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ class TextDecoder {
484484
this[kBOMSeen] = false;
485485
} else if (isSinglebyteEncoding(enc)) {
486486
this[kSingleByte] = createSinglebyteDecoder(enc, this[kFatal]);
487-
} if (hasIntl) {
487+
} else if (hasIntl) {
488488
let icuEncoding = this[kEncoding];
489489
if (icuEncoding === 'gbk') icuEncoding = 'gb18030'; // 10.1.1. GBK's decoder is gb18030's decoder
490490
const handle = icuGetConverter(icuEncoding, this[kFlags]);

0 commit comments

Comments
 (0)