perf(streams): memory optimizations by avoiding large buffer allocation in a loop (#2748)

This commit is contained in:
FUJI Goro 2022-10-05 14:10:54 +09:00 committed by GitHub
parent 385511bc43
commit c5323d365a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -494,14 +494,14 @@ export async function* iterateReader(
},
): AsyncIterableIterator<Uint8Array> {
const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE;
const b = new Uint8Array(bufSize);
while (true) {
const b = new Uint8Array(bufSize);
const result = await r.read(b);
if (result === null) {
break;
}
yield b.subarray(0, result);
yield b.slice(0, result);
}
}
@ -545,14 +545,14 @@ export function* iterateReaderSync(
},
): IterableIterator<Uint8Array> {
const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE;
const b = new Uint8Array(bufSize);
while (true) {
const b = new Uint8Array(bufSize);
const result = r.readSync(b);
if (result === null) {
break;
}
yield b.subarray(0, result);
yield b.slice(0, result);
}
}