Skip to content

Undocumented and possibly wrong behavior of historySize in readline #6336

@vsemozhetbyt

Description

@vsemozhetbyt
  • Subsystem: readline

This is all the docs say about historySize in readline:

historySize - maximum number of history lines retained. Defaults to 30.

If I want to use readline for reading a big file line by line (see example in docs), I don't need the history tracking. However I cannot set historySize to 0:

console.log(
  require('readline').createInterface({
    input: require('fs').createReadStream(__filename, 'utf8'),
    historySize: 0
  }).historySize
);

gives 30;

If I get it right, it is due to this fragment in the readline.js:

historySize = historySize || kHistorySize;

I don't know if failure to distinguish 0 and undefined here is a bug or a feature.

Maybe these fixes could be done:

Tactically:

  1. Clarify in the docs that 0 is not a legal value for historySize.
  2. May be add a throw in the readline.js (currently the function throws only if historySize < 0). Now the user value is silently secretly discarded.

Strategically:

  1. Add the possibility to turn off history tracking by setting historySize to 0. I test some code with historySize: 30 and historySize: 1 — the second case uses slightly less memory, but slightly more time. The history size has less impact on the performance than history tracking itself because of all the operations in the _addHistory function. If I want to read the file with 100000 lines, the 100000 bunches of array operations in this function is a considerable write-off overhead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    readlineIssues and PRs related to the built-in readline module.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions