-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
49 lines (42 loc) · 1.83 KB
/
index.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
import {text, buffer, arrayBuffer} from 'node:stream/consumers';
import getStream, {getStreamAsBuffer, getStreamAsArrayBuffer, getStreamAsArray} from '../source/index.js';
import {createFixture, deleteFixture, FIXTURE_HUMAN_SIZE} from './fixture.js';
import {
createNodeStreamBinary,
createNodeStreamText,
createWebStreamBinary,
createWebStreamText,
} from './stream.js';
import {measureTask} from './measure.js';
const runBenchmarks = async () => {
await createFixture();
try {
await benchmarkNodeStreams(createNodeStreamBinary, `Node.js stream (${FIXTURE_HUMAN_SIZE}, binary)`);
await benchmarkNodeStreams(createNodeStreamText, `Node.js stream (${FIXTURE_HUMAN_SIZE}, text)`);
await benchmarkStreams(createWebStreamBinary, `Web ReadableStream (${FIXTURE_HUMAN_SIZE}, binary)`);
await benchmarkStreams(createWebStreamText, `Web ReadableStream (${FIXTURE_HUMAN_SIZE}, text)`);
} finally {
await deleteFixture();
}
};
const benchmarkNodeStreams = async (createStream, header) => {
await benchmarkStreams(createStream, header);
await logResult('stream.toArray', createStream, stream => stream.toArray());
};
const benchmarkStreams = async (createStream, header) => {
logHeader(header);
await logResult('getStream', createStream, getStream);
await logResult('text', createStream, text);
await logResult('getStreamAsBuffer', createStream, getStreamAsBuffer);
await logResult('buffer', createStream, buffer);
await logResult('getStreamAsArrayBuffer', createStream, getStreamAsArrayBuffer);
await logResult('arrayBuffer', createStream, arrayBuffer);
await logResult('getStreamAsArray', createStream, getStreamAsArray);
};
const logHeader = header => {
console.log(`\n### ${header}\n`);
};
const logResult = async (name, createStream, task) => {
console.log(`- \`${name}()\`: ${await measureTask(createStream, task)}ms`);
};
await runBenchmarks();