Skip to content

Commit cfee7fa

Browse files
gurgundayaduh95
authored andcommitted
stream: amortize writable buffer compaction
Signed-off-by: Gürgün Dayıoğlu <hey@gurgun.day> Assisted-by: Codex PR-URL: #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>
1 parent e3ad682 commit cfee7fa

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { Writable } = require('stream');
5+
6+
const bench = common.createBenchmark(main, {
7+
count: [1024, 16384, 65536],
8+
n: [100],
9+
});
10+
11+
function main({ count, n }) {
12+
const chunk = {};
13+
let callback;
14+
const stream = new Writable({
15+
objectMode: true,
16+
write(chunk, encoding, cb) { callback = cb; },
17+
});
18+
19+
bench.start();
20+
for (let i = 0; i < n; i++) {
21+
for (let j = 0; j < count; j++) stream.write(chunk);
22+
for (let j = 0; j < count; j++) callback();
23+
}
24+
bench.end(n);
25+
}

lib/internal/streams/writable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ function clearBuffer(stream, state) {
787787

788788
if (i === buffered.length) {
789789
resetBuffer(state);
790-
} else if (i > 256) {
790+
} else if (i > 256 && i * 2 >= buffered.length) {
791791
buffered.splice(0, i);
792792
state.bufferedIndex = 0;
793793
} else {

0 commit comments

Comments
 (0)