test.html
1.79 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
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE html>
<html>
<head>
<script defer src="https://cdn.rawgit.com/mathiasbynens/utf8.js/5566334e/utf8.js"></script>
<script type="module">
</script>
<script type="module">
import './polyfill.js';
import 'https://cdn.rawgit.com/inexorabletash/text-encoding/b98ab30b/lib/encoding.js';
const polyfill = module.exports;
import './text.js';
const runs = 1;
const dataUrl = './utf8_sequence_0-0xffff_assigned_printable.txt';
(async function() {
let text = await window.fetch(dataUrl).then((data) => data.text());
text = text.substr(0, 10000);
function testEncodeDecode(name, tenc, tdec) {
console.time(name + '.TextEncoder');
let saved;
const encoder = new tenc();
for (let i = 0; i < runs; ++i) {
const out = encoder.encode(text);
saved = out;
}
console.info('got output', saved);
console.timeEnd(name + '.TextEncoder');
console.time(name + '.TextDecoder');
const decoder = new tdec();
for (let i = 0; i < runs; ++i) {
const out = decoder.decode(saved);
output = out;
}
console.timeEnd(name + '.TextDecoder');
}
let saved, output;
console.time('utf8.encode');
for (let i = 0; i < runs; ++i) {
const s = utf8.encode(text);
saved = s;
}
console.timeEnd('utf8.encode');
console.time('utf8.decode');
for (let i = 0; i < runs; ++i) {
const out = utf8.decode(saved);
output = out;
}
console.timeEnd('utf8.decode');
if (output !== text) {
throw new Error('utf8 got wrong answer');
}
testEncodeDecode('native', TextEncoder, TextDecoder);
testEncodeDecode('fast', TextEncoderPolyfill, TextDecoderPolyfill);
testEncodeDecode('polyfill', polyfill.TextEncoder, polyfill.TextDecoder);
}());
</script>
</head>
</html>