Skip to content

Commit 408e9d3

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 PR-URL: #36880 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 88153dc commit 408e9d3

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

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

6247+
[#25741]: https://github.com/nodejs/node/issues/25741
62256248
[Caveats]: #fs_caveats
62266249
[Common System Errors]: errors.md#errors_common_system_errors
62276250
[FS constants]: #fs_fs_constants_1

0 commit comments

Comments
 (0)