From 1e3a9447958974f0e1a75b65ff1ecbc27a082837 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 19 Sep 2023 16:24:32 +0530 Subject: [PATCH] benchmark: add a benchmark for read() of ReadableStreams Refs: https://github.com/nodejs/performance/issues/82 PR-URL: https://github.com/nodejs/node/pull/49622 Reviewed-By: Yagiz Nizipli --- benchmark/webstreams/readable-read.js | 49 +++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 benchmark/webstreams/readable-read.js diff --git a/benchmark/webstreams/readable-read.js b/benchmark/webstreams/readable-read.js new file mode 100644 index 00000000000000..d3d28dd7f50301 --- /dev/null +++ b/benchmark/webstreams/readable-read.js @@ -0,0 +1,49 @@ +'use strict'; +const common = require('../common.js'); +const { ReadableStream } = require('node:stream/web'); + +const bench = common.createBenchmark(main, { + n: [1e5], + type: ['normal', 'byob'], +}); + +async function main({ n, type }) { + switch (type) { + case 'normal': { + const rs = new ReadableStream({ + pull: function(controller) { + controller.enqueue('a'); + }, + }); + const reader = rs.getReader(); + let x = null; + bench.start(); + for (let i = 0; i < n; i++) { + const { value } = await reader.read(); + x = value; + } + bench.end(n); + console.assert(x); + break; + } + case 'byob': { + const encode = new TextEncoder(); + const rs = new ReadableStream({ + type: 'bytes', + pull: function(controller) { + controller.enqueue(encode.encode('a')); + }, + }); + const reader = rs.getReader({ mode: 'byob' }); + let x = null; + bench.start(); + for (let i = 0; i < n; i++) { + const { value } = await reader.read(new Uint8Array(1)); + x = value; + } + bench.end(n); + console.assert(x); + break; + } + } +}