Skip to content

Commit b25b694

Browse files
jasnelltargos
authored andcommitted
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 PR-URL: #36880 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 385d0df commit b25b694

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
@@ -3126,6 +3126,28 @@ system requests but rather the internal buffering `fs.readFile` performs.
31263126
the call to `fs.readFile()` with the same file descriptor, would give
31273127
`'World'`, rather than `'Hello World'`.
31283128

3129+
### Performance Considerations
3130+
3131+
The `fs.readFile()` method asynchronously reads the contents of a file into
3132+
memory one chunk at a time, allowing the event loop to turn between each chunk.
3133+
This allows the read operation to have less impact on other activity that may
3134+
be using the underlying libuv thread pool but means that it will take longer
3135+
to read a complete file into memory.
3136+
3137+
The additional read overhead can vary broadly on different systems and depends
3138+
on the type of file being read. If the file type is not a regular file (a pipe
3139+
for instance) and Node.js is unable to determine an actual file size, each read
3140+
operation will load on 64kb of data. For regular files, each read will process
3141+
512kb of data.
3142+
3143+
For applications that require as-fast-as-possible reading of file contents, it
3144+
is better to use `fs.read()` directly and for application code to manage
3145+
reading the full contents of the file itself.
3146+
3147+
The Node.js GitHub issue [#25741][] provides more information and a detailed
3148+
analysis on the performance of `fs.readFile()` for multiple file sizes in
3149+
different Node.js versions.
3150+
31293151
## `fs.readFileSync(path[, options])`
31303152
<!-- YAML
31313153
added: v0.1.8
@@ -6152,6 +6174,7 @@ through `fs.open()` or `fs.writeFile()` or `fsPromises.open()`) will fail with
61526174
A call to `fs.ftruncate()` or `filehandle.truncate()` can be used to reset
61536175
the file contents.
61546176

6177+
[#25741]: https://github.com/nodejs/node/issues/25741
61556178
[Caveats]: #fs_caveats
61566179
[Common System Errors]: errors.md#errors_common_system_errors
61576180
[FS constants]: #fs_fs_constants_1

0 commit comments

Comments
 (0)