Skip to content

Commit 5fd5237

Browse files
LinkgoronMoritzLoewenstein
authored andcommitted
fs: improve fsPromises writeFile performance
Increase the write chunk size in fsPromises writeFile to improve performance. PR-URL: nodejs#37610 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent fcf8ba4 commit 5fd5237

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

lib/internal/fs/promises.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
'use strict';
22

3+
// Most platforms don't allow reads or writes >= 2 GB.
4+
// See https://github.com/libuv/libuv/pull/1501.
5+
const kIoMaxLength = 2 ** 31 - 1;
6+
7+
const kReadFileBufferLength = 512 * 1024;
8+
const kReadFileUnknownBufferLength = 64 * 1024;
9+
const kWriteFileMaxChunkSize = 512 * 1024;
10+
311
const {
412
ArrayPrototypePush,
513
Error,

test/parallel/test-fs-promises-file-handle-writeFile.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,13 @@ async function validateWriteFile() {
3030
async function doWriteAndCancel() {
3131
const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt');
3232
const fileHandle = await open(filePathForHandle, 'w+');
33-
try {
34-
const buffer = Buffer.from('dogs running'.repeat(512 * 1024), 'utf8');
35-
const controller = new AbortController();
36-
const { signal } = controller;
37-
process.nextTick(() => controller.abort());
38-
await assert.rejects(writeFile(fileHandle, buffer, { signal }), {
39-
name: 'AbortError'
40-
});
41-
} finally {
42-
await fileHandle.close();
43-
}
33+
const buffer = Buffer.from('dogs running'.repeat(512 * 1024), 'utf8');
34+
const controller = new AbortController();
35+
const { signal } = controller;
36+
process.nextTick(() => controller.abort());
37+
await assert.rejects(writeFile(fileHandle, buffer, { signal }), {
38+
name: 'AbortError'
39+
});
4440
}
4541

4642
validateWriteFile()

0 commit comments

Comments
 (0)