-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperf.ts
91 lines (77 loc) · 4.15 KB
/
perf.ts
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import {
_toHex,
_toHexChunked,
_fromHex,
_fromHexChunked,
_toBase64,
_toBase64Chunked,
_fromBase64,
} from './src/index';
import bufferShimDefault from 'buffer/index.js'; // just 'buffer' imports Node native implementation
export function perf(log = console.log.bind(console)) {
const
BufferShim = bufferShimDefault.Buffer,
length = 33554433,
benchmarkArray = new Uint8Array(length);
for (let i = 0; i < length; i++) benchmarkArray[i] = Math.random() * 256 >>> 0;
const
benchmarkBuffer = typeof Buffer !== 'undefined' ? Buffer.from(benchmarkArray) : null,
benchmarkBufferShim = BufferShim.from(benchmarkArray),
benchmarkBase64Std = _toBase64Chunked(benchmarkArray),
benchmarkHex = _toHexChunked(benchmarkArray);
let iterations = 10;
log(`Benchmarking ${(benchmarkArray.length / 2 ** 20).toFixed(1)} MiB random data, mean of ${iterations} iterations ...`);
log()
function benchmark(fn: () => any, iterations: number) {
try { fn() } catch (err: any) { return ` –` }
const t0 = performance.now();
for (let i = 0; i < iterations; i++) fn();
const t1 = performance.now();
const t = (t1 - t0) / iterations;
const s = t.toFixed(2);
let out = `${' '.repeat(7 - s.length)}${s} ms`;
return out;
}
log('* Encode base64\n')
log(`This library ${benchmark(() => _toBase64Chunked(benchmarkArray), iterations)}`);
// @ts-ignore
log(`cf. native toBase64 ${benchmark(() => benchmarkArray.toBase64(), iterations)}`);
// @ts-ignore
log(`cf. native Buffer.toString ${benchmark(() => benchmarkBuffer.toString('base64'), iterations)}`);
log(`cf. feross/buffer.toString ${benchmark(() => benchmarkBufferShim.toString('base64'), iterations)}`);
log();
log('* Decode base64\n')
log(`This library ${benchmark(() => _fromBase64(benchmarkBase64Std), iterations)}`);
// @ts-ignore
log(`cf. native fromBase64 ${benchmark(() => Uint8Array.fromBase64(benchmarkBase64Std), iterations)}`);
log(`cf. native Buffer.from ${benchmark(() => Buffer.from(benchmarkBase64Std, 'base64'), iterations)}`);
log(`cf. feross/buffer.from ${benchmark(() => BufferShim.from(benchmarkBase64Std, 'base64'), iterations)}`);
log();
log('* Decode base64 with whitespace\n')
log(`This library (strict) ${benchmark(() => _fromBase64(' ' + benchmarkBase64Std, { onInvalidInput: 'throw' }), iterations)}`);
log(`This library (lax) ${benchmark(() => _fromBase64(' ' + benchmarkBase64Std, { onInvalidInput: 'skip' }), iterations)}`);
// @ts-ignore
log(`cf. native fromBase64 ${benchmark(() => Uint8Array.fromBase64(' ' + benchmarkBase64Std), iterations)}`);
log(`cf. native Buffer.from ${benchmark(() => Buffer.from(' ' + benchmarkBase64Std, 'base64'), iterations)}`);
log(`cf. feross/buffer.from ${benchmark(() => BufferShim.from(' ' + benchmarkBase64Std, 'base64'), iterations)}`);
log();
log('* Encode hex\n')
log(`This library ${benchmark(() => _toHexChunked(benchmarkArray), iterations)}`);
// @ts-ignore
log(`cf. native toHex ${benchmark(() => benchmarkArray.toHex(), iterations)}`);
// @ts-ignore
log(`cf. native Buffer.toString ${benchmark(() => benchmarkBuffer.toString('hex'), iterations)}`);
log(`cf. feross/buffer.toString ${benchmark(() => benchmarkBufferShim.toString('hex'), iterations)}`);
log();
log('* Decode hex\n')
log(`This library ${benchmark(() => _fromHexChunked(benchmarkHex), iterations)}`);
// @ts-ignore
log(`cf. native fromHex ${benchmark(() => Uint8Array.fromHex(benchmarkHex), iterations)}`);
log(`cf. native Buffer.from ${benchmark(() => Buffer.from(benchmarkHex, 'hex'), iterations)}`);
log(`cf. feross/buffer.from ${benchmark(() => BufferShim.from(benchmarkHex, 'hex'), iterations)}`);
log();
log('Done.');
return false; // for browser use
}
// @ts-ignore
globalThis.perf = perf; // for browser use