-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbig.js
54 lines (42 loc) · 1.3 KB
/
big.js
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
const { Buffer } = require('node:buffer');
const { decode, encode } = require('../lib/');
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder('utf8');
let string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNIOQRSTUVWXYZ';
for (let i = 0; i < 20; i++) {
string += string;
}
const uint8 = textEncoder.encode(string);
const buffer = Buffer.from(string, 'utf8');
console.time('btoa');
const btoaString = btoa(string);
console.timeEnd('btoa');
console.time('atob');
const atobString = atob(btoaString);
console.timeEnd('atob');
console.time('buffer.toString');
const bufferToString = buffer.toString('base64');
console.timeEnd('buffer.toString');
console.time('Buffer.from');
const bufferFrom = Buffer.from(bufferToString, 'base64');
console.timeEnd('Buffer.from');
console.time('encode');
const bufferBase64 = encode(uint8);
console.timeEnd('encode');
console.time('decode');
const newBuffer = decode(bufferBase64);
console.timeEnd('decode');
const newString = textDecoder.decode(newBuffer);
console.log(
string.length,
uint8.length,
atobString.length,
bufferFrom.length,
bufferBase64.length,
bufferToString.length,
btoaString.length,
btoaString === textDecoder.decode(bufferBase64),
newString === string,
newString === atobString,
newString === textDecoder.decode(bufferFrom),
);