-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbenchmark.mjs
67 lines (47 loc) · 2.33 KB
/
benchmark.mjs
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
/* eslint no-undef: "off" */
import {Buffer} from 'node:buffer';
import {randomBytes} from 'node:crypto';
import benchmark from 'benchmark';
import {
base64ToString,
compareUint8Arrays,
concatUint8Arrays,
hexToUint8Array,
isUint8Array,
stringToBase64,
stringToUint8Array,
uint8ArrayToBase64,
uint8ArrayToHex,
uint8ArrayToString,
getUintBE,
indexOf,
includes,
} from './index.js';
const oneMb = 1024 * 1024;
const largeUint8Array = new Uint8Array(randomBytes(oneMb).buffer);
// eslint-disable-next-line unicorn/prefer-spread
const largeUint8ArrayDuplicate = largeUint8Array.slice();
const partOfUint8Array = largeUint8Array.slice(-1024);
const textFromUint8Array = uint8ArrayToString(largeUint8Array);
const base64FromUint8Array = Buffer.from(textFromUint8Array).toString('base64');
const hexFromUint8Array = uint8ArrayToHex(largeUint8Array);
const view = new DataView(largeUint8Array.buffer, 0, 10);
const suite = new benchmark.Suite();
suite.add('isUint8Array', () => isUint8Array(largeUint8Array));
suite.add('compareUint8Arrays', () => compareUint8Arrays(largeUint8Array, largeUint8ArrayDuplicate));
suite.add('concatUint8Arrays with 2 arrays', () => concatUint8Arrays([largeUint8Array, largeUint8Array]));
suite.add('concatUint8Arrays with 3 arrays', () => concatUint8Arrays([largeUint8Array, largeUint8Array]));
suite.add('concatUint8Arrays with 4 arrays', () => concatUint8Arrays([largeUint8Array, largeUint8Array, largeUint8Array, largeUint8Array]));
suite.add('uint8ArrayToString', () => uint8ArrayToString(largeUint8Array));
suite.add('uint8ArrayToString - latin1', () => uint8ArrayToString(largeUint8Array, 'latin1'));
suite.add('stringToUint8Array', () => stringToUint8Array(textFromUint8Array));
suite.add('uint8ArrayToBase64', () => uint8ArrayToBase64(largeUint8Array));
suite.add('stringToBase64', () => stringToBase64(textFromUint8Array));
suite.add('base64ToString', () => base64ToString(base64FromUint8Array));
suite.add('uint8ArrayToHex', () => uint8ArrayToHex(largeUint8Array));
suite.add('hexToUint8Array', () => hexToUint8Array(hexFromUint8Array));
suite.add('getUintBE', () => getUintBE(view));
suite.add('indexOf', () => indexOf(largeUint8Array, partOfUint8Array));
suite.add('includes', () => includes(largeUint8Array, partOfUint8Array));
suite.on('cycle', event => console.log(event.target.toString()));
suite.run({async: false});