GeezerVison is showing github.com — doc: add caveat and tradeoff example to readline · nodejs/node@2ee9a96
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: doc/api/readline.md +28Lines changed: 28 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -597,6 +597,34 @@ rl.on('line', (line) => { |
597 | 597 | }); |
598 | 598 | ``` |
599 | 599 |
|
| 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 | + |
600 | 628 | [`'SIGCONT'`]: readline.html#readline_event_sigcont |
601 | 629 | [`'SIGTSTP'`]: readline.html#readline_event_sigtstp |
602 | 630 | [`'line'`]: #readline_event_line |
|