mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
The encode-and-enqueue transform walked the chunk code unit by code
unit, materializing a single-character string per index and building
the output with string concatenation. The only state that crosses
chunks is a trailing high (leading) surrogate, and TextEncoder.encode's
USVString conversion already replaces every interior lone surrogate
with U+FFFD, which is exactly what the spec loop produces. Join a
pending high surrogate with the incoming chunk, hold back a new
trailing high surrogate, and encode the rest in a single native call.
The streaming decode path also reuses a single options object instead
of allocating { stream: true } per chunk.
An encoding-streams benchmark is added since the suite had no
TextEncoderStream/TextDecoderStream row. Encoding improves by ~546%
with 1KB string chunks and ~20% with 16-character chunks; decode is
unchanged.
Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/65414
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Mattias Buelens <mattias@buelens.com>
40 lines
881 B
JavaScript
40 lines
881 B
JavaScript
'use strict';
|
|
const common = require('../common.js');
|
|
const {
|
|
ReadableStream,
|
|
TextEncoderStream,
|
|
TextDecoderStream,
|
|
} = require('node:stream/web');
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
n: [1e5],
|
|
kind: ['encode', 'decode'],
|
|
len: [16, 1024],
|
|
});
|
|
|
|
async function main({ n, kind, len }) {
|
|
const encoded = new TextEncoder().encode('a'.repeat(len));
|
|
const decoded = 'a'.repeat(len);
|
|
let i = 0;
|
|
const rs = new ReadableStream({
|
|
pull(controller) {
|
|
if (i++ < n) {
|
|
controller.enqueue(kind === 'encode' ? decoded : encoded);
|
|
} else {
|
|
controller.close();
|
|
}
|
|
},
|
|
});
|
|
const ts = kind === 'encode' ?
|
|
new TextEncoderStream() :
|
|
new TextDecoderStream();
|
|
|
|
const reader = rs.pipeThrough(ts).getReader();
|
|
bench.start();
|
|
for (;;) {
|
|
const { done } = await reader.read();
|
|
if (done) break;
|
|
}
|
|
bench.end(n);
|
|
}
|