Skip to content

Commit f857c23

Browse files
committed
stream: make stream/iter transform benchmark better
1 parent 46ab61c commit f857c23

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

benchmark/streams/iter-throughput-transform.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Throughput benchmark: data flow through a single stateless transform.
2-
// Uses byte-level ASCII uppercase (branchless, no encoding dependency).
2+
// Uses buffer copy (allocate + memcpy) so pipeline overhead is measurable.
33
'use strict';
44

55
const common = require('../common.js');
@@ -15,13 +15,11 @@ const bench = common.createBenchmark(main, {
1515

1616
const CHUNK_SIZE = 64 * 1024;
1717

18-
// Byte-level ASCII uppercase: branchless, no string encoding.
19-
function upperBuf(buf) {
20-
const out = Buffer.allocUnsafe(buf.length);
21-
for (let i = 0; i < buf.length; i++) {
22-
out[i] = buf[i] - (buf[i] >= 97 && buf[i] <= 122) * 32;
23-
}
24-
return out;
18+
// Buffer copy transform: allocate + memcpy. Cheap enough that pipeline
19+
// overhead is a measurable fraction of total time, but non-trivial (new
20+
// buffer per chunk, so it's a real transform that produces new data).
21+
function copyBuf(buf) {
22+
return Buffer.copyBytesFrom(buf);
2523
}
2624

2725
function main({ api, datasize, n }) {
@@ -53,7 +51,7 @@ function benchClassic(chunk, datasize, n, totalOps) {
5351
});
5452
const t = new Transform({
5553
transform(data, enc, cb) {
56-
cb(null, upperBuf(data));
54+
cb(null, copyBuf(data));
5755
},
5856
});
5957
const w = new Writable({ write(data, enc, cb) { cb(); } });
@@ -82,7 +80,7 @@ function benchWebStream(chunk, datasize, n, totalOps) {
8280
});
8381
const ts = new TransformStream({
8482
transform(c, controller) {
85-
controller.enqueue(upperBuf(c));
83+
controller.enqueue(copyBuf(c));
8684
},
8785
});
8886
const ws = new WritableStream({ write() {} });
@@ -101,7 +99,7 @@ function benchIter(chunk, datasize, n, totalOps) {
10199

102100
const upper = (chunks) => {
103101
if (chunks === null) return null;
104-
return chunks.map((c) => upperBuf(c));
102+
return chunks.map((c) => copyBuf(c));
105103
};
106104

107105
async function run() {
@@ -129,7 +127,7 @@ function benchIterSync(chunk, datasize, n, totalOps) {
129127

130128
const upper = (chunks) => {
131129
if (chunks === null) return null;
132-
return chunks.map((c) => upperBuf(c));
130+
return chunks.map((c) => copyBuf(c));
133131
};
134132

135133
bench.start();

0 commit comments

Comments
 (0)