Skip to content

Commit d05758f

Browse files
meixgsxa
authored andcommitted
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 0413736 commit d05758f

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
@@ -65,6 +65,7 @@ const {
6565

6666
const kHandle = Symbol('kHandle');
6767
const kState = Symbol('kState');
68+
const kIndex = Symbol('kIndex');
6869
const kType = Symbol('kType');
6970
const kLength = Symbol('kLength');
7071
const kArrayBufferPromise = Symbol('kArrayBufferPromise');
@@ -325,17 +326,17 @@ class Blob {
325326
return new lazyReadableStream({
326327
async start() {
327328
this[kState] = await self.arrayBuffer();
329+
this[kIndex] = 0;
328330
},
329331

330332
pull(controller) {
331-
if (this[kState].byteLength <= kMaxChunkSize) {
332-
controller.enqueue(new Uint8Array(this[kState]));
333+
if (this[kState].byteLength - this[kIndex] <= kMaxChunkSize) {
334+
controller.enqueue(new Uint8Array(this[kState], this[kIndex]));
333335
controller.close();
334336
this[kState] = undefined;
335337
} else {
336-
const slice = this[kState].slice(0, kMaxChunkSize);
337-
this[kState] = this[kState].slice(kMaxChunkSize);
338-
controller.enqueue(new Uint8Array(slice));
338+
controller.enqueue(new Uint8Array(this[kState], this[kIndex], kMaxChunkSize));
339+
this[kIndex] += kMaxChunkSize;
339340
}
340341
}
341342
});

0 commit comments

Comments
 (0)