Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fs: read full size if known in promises.readFile
If we have an estimate of the file size available from the previous
stat call, use that for the size of the first chunk to be read.
This increases performance by reading more data (and, most likely,
all data) at once without incurring memory overhead in most situations.
  • Loading branch information
addaleax committed Jan 29, 2021
commit 5ce769e2381631f23d56b4c01c8065f661695515
11 changes: 6 additions & 5 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,20 +315,21 @@ async function readFileHandle(filehandle, options) {
throw new ERR_FS_FILE_TOO_LARGE(size);

const chunks = [];
const chunkSize = size === 0 ?
kReadFileMaxChunkSize :
MathMin(size, kReadFileMaxChunkSize);
let isFirstChunk = true;
const firstChunkSize = size === 0 ? kReadFileMaxChunkSize : size;
const chunkSize = Math.min(firstChunkSize, kReadFileMaxChunkSize);
Comment thread
addaleax marked this conversation as resolved.
Resolved
let endOfFile = false;
do {
if (signal?.aborted) {
throw lazyDOMException('The operation was aborted', 'AbortError');
}
const buf = Buffer.alloc(chunkSize);
const buf = Buffer.alloc(isFirstChunk ? firstChunkSize : chunkSize);
const { bytesRead, buffer } =
await read(filehandle, buf, 0, chunkSize, -1);
await read(filehandle, buf, 0, buf.length, -1);
endOfFile = bytesRead === 0;
if (bytesRead > 0)
ArrayPrototypePush(chunks, buffer.slice(0, bytesRead));
isFirstChunk = false;
} while (!endOfFile);

const result = chunks.length === 1 ? chunks[0] : Buffer.concat(chunks);
Expand Down