Skip to content

Commit 3e0126a

Browse files
authored
perf: avoid unnecessary buffer copy in internalWrite (#1013)
1 parent 0e29718 commit 3e0126a

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

.changeset/olive-cars-count.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
perf: avoid unnecessary buffer copy in internalWrite

packages/open-next/src/http/openNextResponse.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,18 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
285285
}
286286

287287
private _internalWrite(chunk: any, encoding: BufferEncoding) {
288-
const buffer = Buffer.from(chunk, encoding);
288+
// When encoding === 'buffer', chunk is already a Buffer
289+
// and does not need to be converted again.
290+
// @ts-expect-error TS2367 'encoding' can be 'buffer', but it's not in the
291+
// official type definition
292+
const buffer = encoding === "buffer" ? chunk : Buffer.from(chunk, encoding);
289293
this.bodyLength += buffer.length;
290294
if (this.streamCreator?.retainChunks !== false) {
291295
// Avoid keeping chunks around when the `StreamCreator` supports it to save memory
292296
this._chunks.push(buffer);
293297
}
294-
this.push(chunk, encoding);
298+
// No need to pass the encoding for buffers
299+
this.push(buffer);
295300
this.streamCreator?.onWrite?.();
296301
}
297302

0 commit comments

Comments
 (0)