From d40f5a177a45c649c8bd3c85ac4fab44d43ee228 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 6c1388744b17d4..a620552d846051 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; } } });