Skip to content

Commit 92d731d

Browse files
committed
doc: add performance notes for fs.readFile
Issue #25741 discusses a number of performance considerations for fs.readFile, which was changed in Node.js 10.x to split discreet chunk reads over multiple event loop turns. While the fs.readFile() operation is certainly slower than it was pre 10.x, it's unlikely to be faster. Document the performance consideration and link back to the issue for more in depth analysis. Signed-off-by: James M Snell <jasnell@gmail.com> Fixes: #25741
1 parent 03c0564 commit 92d731d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

doc/api/fs.md

+23
Original file line numberDiff line numberDiff line change
@@ -3162,6 +3162,28 @@ system requests but rather the internal buffering `fs.readFile` performs.
31623162
the call to `fs.readFile()` with the same file descriptor, would give
31633163
`'World'`, rather than `'Hello World'`.
31643164

3165+
### Performance Considerations
3166+
3167+
The `fs.readFile()` method asynchronously reads the contents of a file into
3168+
memory one chunk at a time, allowing the event loop to turn between each chunk.
3169+
This allows the read operation to have less impact on other activity that may
3170+
be using the underlying libuv thread pool but means that it will take longer
3171+
to read a complete file into memory.
3172+
3173+
The additional read overhead can vary broadly on different systems and depends
3174+
on the type of file being read. If the file type is not a regular file (a pipe
3175+
for instance) and Node.js is unable to determine an actual file size, each read
3176+
operation will load on 64kb of data. For regular files, each read will process
3177+
512kb of data.
3178+
3179+
For applications that require as-fast-as-possible reading of file contents, it
3180+
is better to use `fs.read()` directly and for application code to manage
3181+
reading the full contents of the file itself.
3182+
3183+
The Node.js GitHub issue [#25741][] provides more information and a detailed
3184+
analysis on the performance of `fs.readFile()` for multiple file sizes in
3185+
different Node.js versions.
3186+
31653187
## `fs.readFileSync(path[, options])`
31663188
<!-- YAML
31673189
added: v0.1.8
@@ -6214,6 +6236,7 @@ through `fs.open()` or `fs.writeFile()` or `fsPromises.open()`) will fail with
62146236
A call to `fs.ftruncate()` or `filehandle.truncate()` can be used to reset
62156237
the file contents.
62166238

6239+
[#25741]: https://github.com/nodejs/node/issues/25741
62176240
[Caveats]: #fs_caveats
62186241
[Common System Errors]: errors.md#errors_common_system_errors
62196242
[FS constants]: #fs_fs_constants_1

0 commit comments

Comments
 (0)