Skip to content

Commit

Permalink
buffer: improve blob read performance
Browse files Browse the repository at this point in the history
Fix: #42108

PR-URL: #42117
Fixes: #42108
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Mestery <[email protected]>
  • Loading branch information
meixg authored and sxa committed Mar 7, 2022
1 parent 0413736 commit d05758f
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const {

const kHandle = Symbol('kHandle');
const kState = Symbol('kState');
const kIndex = Symbol('kIndex');
const kType = Symbol('kType');
const kLength = Symbol('kLength');
const kArrayBufferPromise = Symbol('kArrayBufferPromise');
Expand Down Expand Up @@ -325,17 +326,17 @@ class Blob {
return new lazyReadableStream({
async start() {
this[kState] = await self.arrayBuffer();
this[kIndex] = 0;
},

pull(controller) {
if (this[kState].byteLength <= kMaxChunkSize) {
controller.enqueue(new Uint8Array(this[kState]));
if (this[kState].byteLength - this[kIndex] <= kMaxChunkSize) {
controller.enqueue(new Uint8Array(this[kState], this[kIndex]));
controller.close();
this[kState] = undefined;
} else {
const slice = this[kState].slice(0, kMaxChunkSize);
this[kState] = this[kState].slice(kMaxChunkSize);
controller.enqueue(new Uint8Array(slice));
controller.enqueue(new Uint8Array(this[kState], this[kIndex], kMaxChunkSize));
this[kIndex] += kMaxChunkSize;
}
}
});
Expand Down

0 comments on commit d05758f

Please sign in to comment.