-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
Expand file tree
/
Copy pathtext-decoder.js
More file actions
58 lines (52 loc) · 1.39 KB
/
text-decoder.js
File metadata and controls
58 lines (52 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
const common = require('../common.js');
const bench = common.createBenchmark(main, {
encoding: ['utf-8', 'windows-1252', 'iso-8859-3'],
ignoreBOM: [0, 1],
fatal: [0, 1],
type: ['SharedArrayBuffer', 'ArrayBuffer', 'Buffer'],
content: ['ascii', 'one-byte-string', 'two-byte-string'],
len: [256, 1024 * 16, 1024 * 128],
n: [1e3],
});
function buildContent(content, len) {
let base;
switch (content) {
case 'ascii': base = 'a'; break;
case 'one-byte-string': base = '\xff'; break;
case 'two-byte-string': base = 'ğ'; break;
}
const unitBytes = Buffer.byteLength(base, 'utf8');
const copies = Math.max(1, Math.floor(len / unitBytes));
return Buffer.from(base.repeat(copies));
}
function main({ encoding, len, n, ignoreBOM, type, fatal, content }) {
const decoder = new TextDecoder(encoding, { ignoreBOM, fatal });
const seed = buildContent(content, len);
let buf;
switch (type) {
case 'SharedArrayBuffer': {
buf = new SharedArrayBuffer(seed.length);
new Uint8Array(buf).set(seed);
break;
}
case 'ArrayBuffer': {
buf = new ArrayBuffer(seed.length);
new Uint8Array(buf).set(seed);
break;
}
case 'Buffer': {
buf = seed;
break;
}
}
bench.start();
for (let i = 0; i < n; i++) {
try {
decoder.decode(buf);
} catch {
// eslint-disable no-empty
}
}
bench.end(n);
}