Skip to content

Commit 4065785

Browse files
vsemozhetbytBethGriggs
authored andcommitted
doc: add caveat and tradeoff example to readline
PR-URL: #26472 Refs: #23916 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 479ef60 commit 4065785

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

doc/api/readline.md

+28
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,34 @@ rl.on('line', (line) => {
534534
});
535535
```
536536

537+
Currently, `for`-`await`-`of` loop can be a bit slower. If `async` / `await`
538+
flow and speed are both essential, a mixed approach can be applied:
539+
540+
```js
541+
const { once } = require('events');
542+
const { createReadStream } = require('fs');
543+
const { createInterface } = require('readline');
544+
545+
(async function processLineByLine() {
546+
try {
547+
const rl = createInterface({
548+
input: createReadStream('big-file.txt'),
549+
crlfDelay: Infinity
550+
});
551+
552+
rl.on('line', (line) => {
553+
// Process the line.
554+
});
555+
556+
await once(rl, 'close');
557+
558+
console.log('File processed.');
559+
} catch (err) {
560+
console.error(err);
561+
}
562+
})();
563+
```
564+
537565
[`'SIGCONT'`]: readline.html#readline_event_sigcont
538566
[`'SIGTSTP'`]: readline.html#readline_event_sigtstp
539567
[`process.stdin`]: process.html#process_process_stdin

0 commit comments

Comments
 (0)