From d05758f79f96e51c9ebb243d339dfad44bf18dca Mon Sep 17 00:00:00 2001 From: Xuguang Mei Date: Mon, 28 Feb 2022 02:48:47 +0800 Subject: [PATCH] buffer: improve blob read performance Fix: https://github.com/nodejs/node/issues/42108 PR-URL: https://github.com/nodejs/node/pull/42117 Fixes: https://github.com/nodejs/node/issues/42108 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Mestery --- lib/internal/blob.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/internal/blob.js b/lib/internal/blob.js index e5ea99b0a965d2..d35a4615665c4d 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -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'); @@ -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; } } });