mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 18:25:13 +08: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>
30 lines
596 B
JavaScript
30 lines
596 B
JavaScript
'use strict';
|
|
const common = require('../common.js');
|
|
const {
|
|
ReadableStream,
|
|
} = require('node:stream/web');
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
n: [1e6],
|
|
kind: ['sync', 'async'],
|
|
});
|
|
|
|
async function main({ n, kind }) {
|
|
function* syncGen() {
|
|
for (let i = 0; i < n; i++) yield i;
|
|
}
|
|
|
|
async function* asyncGen() {
|
|
for (let i = 0; i < n; i++) yield i;
|
|
}
|
|
|
|
const reader = ReadableStream.from(
|
|
kind === 'sync' ? syncGen() : asyncGen()).getReader();
|
|
bench.start();
|
|
for (;;) {
|
|
const { done } = await reader.read();
|
|
if (done) break;
|
|
}
|
|
bench.end(n);
|
|
}
|