stream: amortize writable buffer compaction

Signed-off-by: Gürgün Dayıoğlu <hey@gurgun.day>
Assisted-by: Codex
PR-URL: https://github.com/nodejs/node/pull/65847
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Xuguang Mei <meixuguang@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Gürgün Dayıoğlu authored and Antoine du Hamel committed 2026-09-16 21:12:06 +02:00
1 parent e3ad68289a
commit cfee7fa3d9
2 files changed
+26 -1

No files matched your search

@@ -0,0 +1,25 @@
'use strict';
const common = require('../common');
const { Writable } = require('stream');
const bench = common.createBenchmark(main, {
count: [1024, 16384, 65536],
n: [100],
});
function main({ count, n }) {
const chunk = {};
let callback;
const stream = new Writable({
objectMode: true,
write(chunk, encoding, cb) { callback = cb; },
});
bench.start();
for (let i = 0; i < n; i++) {
for (let j = 0; j < count; j++) stream.write(chunk);
for (let j = 0; j < count; j++) callback();
}
bench.end(n);
}
+1 -1
View File
@@ -787,7 +787,7 @@ function clearBuffer(stream, state) {
if (i === buffered.length) {
resetBuffer(state);
} else if (i > 256) {
} else if (i > 256 && i * 2 >= buffered.length) {
buffered.splice(0, i);
state.bufferedIndex = 0;
} else {