Skip to content

Commit 38626e7

Browse files
authored
buffer: improve blob read performance
Fix: #42108 PR-URL: #42117 Fixes: #42108 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Mestery <[email protected]>
1 parent 04c68ba commit 38626e7

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

lib/internal/blob.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const {
6464

6565
const kHandle = Symbol('kHandle');
6666
const kState = Symbol('kState');
67+
const kIndex = Symbol('kIndex');
6768
const kType = Symbol('kType');
6869
const kLength = Symbol('kLength');
6970
const kArrayBufferPromise = Symbol('kArrayBufferPromise');
@@ -323,17 +324,17 @@ class Blob {
323324
return new lazyReadableStream({
324325
async start() {
325326
this[kState] = await self.arrayBuffer();
327+
this[kIndex] = 0;
326328
},
327329

328330
pull(controller) {
329-
if (this[kState].byteLength <= kMaxChunkSize) {
330-
controller.enqueue(new Uint8Array(this[kState]));
331+
if (this[kState].byteLength - this[kIndex] <= kMaxChunkSize) {
332+
controller.enqueue(new Uint8Array(this[kState], this[kIndex]));
331333
controller.close();
332334
this[kState] = undefined;
333335
} else {
334-
const slice = this[kState].slice(0, kMaxChunkSize);
335-
this[kState] = this[kState].slice(kMaxChunkSize);
336-
controller.enqueue(new Uint8Array(slice));
336+
controller.enqueue(new Uint8Array(this[kState], this[kIndex], kMaxChunkSize));
337+
this[kIndex] += kMaxChunkSize;
337338
}
338339
}
339340
});

0 commit comments

Comments
 (0)