Skip to content

Commit 93fc295

Browse files
jasnellruyadorno
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 5290d63 commit 93fc295

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

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

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

0 commit comments

Comments
 (0)