0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-22 07:37:56 +01:00
nodejs/benchmark/webstreams/js_transfer.js
Vinicius Lourenço a85e4186e5
stream: reduce overhead of transfer
PR-URL: https://github.com/nodejs/node/pull/50107
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
2023-10-12 14:37:41 +00:00

53 lines
1.1 KiB
JavaScript

'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();
}