Skip to content

Commit 15e741a

Browse files
committed
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 9f1282d commit 15e741a

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
@@ -597,6 +597,34 @@ rl.on('line', (line) => {
597597
});
598598
```
599599

600+
Currently, `for`-`await`-`of` loop can be a bit slower. If `async` / `await`
601+
flow and speed are both essential, a mixed approach can be applied:
602+
603+
```js
604+
const { once } = require('events');
605+
const { createReadStream } = require('fs');
606+
const { createInterface } = require('readline');
607+
608+
(async function processLineByLine() {
609+
try {
610+
const rl = createInterface({
611+
input: createReadStream('big-file.txt'),
612+
crlfDelay: Infinity
613+
});
614+
615+
rl.on('line', (line) => {
616+
// Process the line.
617+
});
618+
619+
await once(rl, 'close');
620+
621+
console.log('File processed.');
622+
} catch (err) {
623+
console.error(err);
624+
}
625+
})();
626+
```
627+
600628
[`'SIGCONT'`]: readline.html#readline_event_sigcont
601629
[`'SIGTSTP'`]: readline.html#readline_event_sigtstp
602630
[`'line'`]: #readline_event_line

0 commit comments

Comments
 (0)