-
Notifications
You must be signed in to change notification settings - Fork 30.5k
/
Copy pathjs_transfer.js
52 lines (41 loc) · 1.1 KB
/
js_transfer.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
'use strict';
const common = require('../common.js');
const { MessageChannel } = require('worker_threads');
const { WritableStream, TransformStream, ReadableStream } = require('stream/web');
const bench = common.createBenchmark(main, {
payload: ['WritableStream', 'ReadableStream', 'TransformStream'],
n: [1e4],
});
function main({ n, payload: payloadType }) {
let createPayload;
let messages = 0;
switch (payloadType) {
case 'WritableStream':
createPayload = () => new WritableStream();
break;
case 'ReadableStream':
createPayload = () => new ReadableStream();
break;
case 'TransformStream':
createPayload = () => new TransformStream();
break;
default:
throw new Error('Unsupported payload type');
}
const { port1, port2 } = new MessageChannel();
port2.onmessage = onMessage;
function onMessage() {
if (messages++ === n) {
bench.end(n);
port1.close();
} else {
send();
}
}
function send() {
const stream = createPayload();
port1.postMessage(stream, [stream]);
}
bench.start();
send();
}