Skip to content

Commit 03d36bb

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 55493f2 commit 03d36bb

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

lib/internal/fs/promises.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
const kWriteFileMaxChunkSize = 2 ** 14;
4-
53
const {
64
ArrayPrototypePush,
75
Error,
@@ -40,6 +38,7 @@ const {
4038
kMaxUserId,
4139
kReadFileBufferLength,
4240
kReadFileUnknownBufferLength,
41+
kWriteFileMaxChunkSize,
4342
},
4443
copyObject,
4544
getDirents,

lib/internal/fs/utils.js

+3
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ const kIoMaxLength = 2 ** 31 - 1;
126126
const kReadFileUnknownBufferLength = 64 * 1024;
127127
const kReadFileBufferLength = 512 * 1024;
128128

129+
const kWriteFileMaxChunkSize = 512 * 1024;
130+
129131
const kMaxUserId = 2 ** 32 - 1;
130132

131133
const isWindows = process.platform === 'win32';
@@ -835,6 +837,7 @@ module.exports = {
835837
kMaxUserId,
836838
kReadFileBufferLength,
837839
kReadFileUnknownBufferLength,
840+
kWriteFileMaxChunkSize,
838841
},
839842
assertEncoding,
840843
BigIntStats, // for testing

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

+11-7
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ async function validateWriteFile() {
3434
async function doWriteAndCancel() {
3535
const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt');
3636
const fileHandle = await open(filePathForHandle, 'w+');
37-
const buffer = Buffer.from('dogs running'.repeat(10000), 'utf8');
38-
const controller = new AbortController();
39-
const { signal } = controller;
40-
process.nextTick(() => controller.abort());
41-
await assert.rejects(writeFile(fileHandle, buffer, { signal }), {
42-
name: 'AbortError'
43-
});
37+
try {
38+
const buffer = Buffer.from('dogs running'.repeat(512 * 1024), 'utf8');
39+
const controller = new AbortController();
40+
const { signal } = controller;
41+
process.nextTick(() => controller.abort());
42+
await assert.rejects(writeFile(fileHandle, buffer, { signal }), {
43+
name: 'AbortError'
44+
});
45+
} finally {
46+
await fileHandle.close();
47+
}
4448
}
4549

4650
const dest = path.resolve(tmpDir, 'tmp.txt');

0 commit comments

Comments
 (0)