-
Notifications
You must be signed in to change notification settings - Fork 5
/
sync-vs-async.js
executable file
·78 lines (64 loc) · 1.81 KB
/
sync-vs-async.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
/* eslint-disable no-await-in-loop */
const prettyBytes = require('pretty-bytes');
const Table = require('cli-table');
const timeSpan = require('time-span');
const minimist = require('minimist');
const b64 = require('../');
const argv = minimist(process.argv.slice(2));
const chunkSize = argv.chunkSize || 250000;
const bytesToBenchmark = (
argv.bytesToBenchmark &&
argv.bytesToBenchmark.split(',').map(Number)
) || [10000, 100000, 1000000, 10000000, 100000000];
const log = text => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(text);
};
const bench = noOfBytes => Promise.resolve().then(async () => {
const results = {};
const humanBytes = prettyBytes(noOfBytes);
const buffer = Buffer.alloc(noOfBytes);
let end;
log(`${humanBytes}: Encoding sync...`);
end = timeSpan();
const bufferBase64 = buffer.toString('base64');
results.encodeSync = end();
log(`${humanBytes}: Decoding sync...`);
end = timeSpan();
Buffer.from(bufferBase64, 'base64');
results.decodeSync = end();
log(`${humanBytes}: Encoding async...`);
end = timeSpan();
await b64(buffer, { chunkSize });
results.encodeAsync = end();
log(`${humanBytes}: Decoding async...`);
end = timeSpan();
await b64(bufferBase64, { chunkSize });
results.decodeAsync = end();
return results;
});
(async () => {
const table = new Table({
head: [
'Bytes',
'Encode Sync',
'Decode Sync',
'Encode Async',
'Decode Async'
]
});
for (const noOfBytes of bytesToBenchmark) {
const results = await bench(noOfBytes);
table.push([
prettyBytes(noOfBytes),
results.encodeSync + 'ms',
results.decodeSync + 'ms',
results.encodeAsync + 'ms',
results.decodeAsync + 'ms'
]);
}
log(`Benchmark completed with a chunk size of ${prettyBytes(chunkSize)}\n`);
console.log(table.toString());
})();