From 87ee722873df37325fdce7d8a64e35ab14cfee3e Mon Sep 17 00:00:00 2001 From: jakecastelli <38635403+jakecastelli@users.noreply.github.com> Date: Mon, 12 Aug 2024 22:10:04 +0930 Subject: [PATCH] benchmark: add stream.compose benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/54308 Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: Yagiz Nizipli Reviewed-By: James M Snell --- benchmark/streams/compose.js | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 benchmark/streams/compose.js diff --git a/benchmark/streams/compose.js b/benchmark/streams/compose.js new file mode 100644 index 00000000000..b98596ffbd1 --- /dev/null +++ b/benchmark/streams/compose.js @@ -0,0 +1,42 @@ +'use strict'; +const common = require('../common.js'); + +const { + PassThrough, + Readable, + Writable, + compose, +} = require('node:stream'); + +const bench = common.createBenchmark(main, { + n: [1e3], +}); + +function main({ n }) { + const cachedPassThroughs = []; + const cachedReadables = []; + const cachedWritables = []; + + for (let i = 0; i < n; i++) { + const numberOfPassThroughs = 100; + const passThroughs = []; + + for (let i = 0; i < numberOfPassThroughs; i++) { + passThroughs.push(new PassThrough()); + } + + const readable = Readable.from(['hello', 'world']); + const writable = new Writable({ objectMode: true, write: (chunk, encoding, cb) => cb() }); + + cachedPassThroughs.push(passThroughs); + cachedReadables.push(readable); + cachedWritables.push(writable); + } + + bench.start(); + for (let i = 0; i < n; i++) { + const composed = compose(cachedReadables[i], ...cachedPassThroughs[i], cachedWritables[i]); + composed.end(); + } + bench.end(n); +}