Skip to content

Commit e51245b

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 f0b7b93 commit e51245b

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

lib/internal/fs/promises.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
// See https://github.com./libuv/libuv/pull/1501.
55
const kIoMaxLength = 2 ** 31 - 1;
66

7-
// Note: This is different from kReadFileBufferLength used for non-promisified
8-
// fs.readFile.
9-
const kReadFileMaxChunkSize = 2 ** 14;
10-
const kWriteFileMaxChunkSize = 2 ** 14;
7+
const kReadFileBufferLength = 512 * 1024;
8+
const kReadFileUnknownBufferLength = 64 * 1024;
119

1210
// 2 ** 32 - 1
1311
const kMaxUserId = 4294967295;
@@ -44,6 +42,9 @@ const {
4442
const { isArrayBufferView } = require('internal/util/types');
4543
const { rimrafPromises } = require('internal/fs/rimraf');
4644
const {
45+
constants: {
46+
kWriteFileMaxChunkSize
47+
},
4748
copyObject,
4849
getDirents,
4950
getOptions,

lib/internal/fs/utils.js

+5
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ const kMaximumCopyMode = COPYFILE_EXCL |
113113
COPYFILE_FICLONE |
114114
COPYFILE_FICLONE_FORCE;
115115

116+
const kWriteFileMaxChunkSize = 512 * 1024;
117+
116118
const isWindows = process.platform === 'win32';
117119

118120
let fs;
@@ -815,6 +817,9 @@ const validatePosition = hideStackFrames((position, name) => {
815817
});
816818

817819
module.exports = {
820+
constants: {
821+
kWriteFileMaxChunkSize
822+
},
818823
assertEncoding,
819824
BigIntStats, // for testing
820825
copyObject,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async function validateWriteFile() {
3131
async function doWriteAndCancel() {
3232
const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt');
3333
const fileHandle = await open(filePathForHandle, 'w+');
34-
const buffer = Buffer.from('dogs running'.repeat(10000), 'utf8');
34+
const buffer = Buffer.from('dogs running'.repeat(512 * 1024), 'utf8');
3535
const controller = new AbortController();
3636
const { signal } = controller;
3737
process.nextTick(() => controller.abort());

0 commit comments

Comments
 (0)