Repository navigation
Conversation
Speed up Interface construction: - Hoist the history accessor property descriptors to module scope and define them with a single ObjectDefineProperties call, instead of allocating six closures and four descriptor objects per instance. - Stop assigning the history options onto the input stream. This avoids hidden class transitions on the user provided stream and no longer mutates it observably. - Only check process.env.TERM for a dumb terminal when the interface is in terminal mode. Reading process.env goes through the environment interceptor and is comparatively expensive, and _ttyWrite is never called when terminal is false. Signed-off-by: Matteo Collina <hello@matteocollina.com>
Collaborator
|
Review requested:
|
Replace the per-line regular expression exec loop with a single split pass, and use a plain string split on "\n" when the chunk contains none of the rare line endings (\r, \u2028, \u2029). This avoids allocating a match object and two index entries per line and lets the common case use the fast string split path. readline/readline-iterable.js type='new' improves by up to 80% and the event based line consumption by up to 65%. Signed-off-by: Matteo Collina <hello@matteocollina.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #64585 +/- ##
==========================================
- Coverage 90.13% 90.12% -0.02%
==========================================
Files 741 741
Lines 241976 241994 +18
Branches 45543 45551 +8
==========================================
- Hits 218116 218101 -15
- Misses 15379 15410 +31
- Partials 8481 8483 +2
🚀 New features to boost your workflow:
|
jasnell
approved these changes
Jul 19, 2026
RafaelGSS
approved these changes
Jul 19, 2026
anonrig
approved these changes
Jul 19, 2026
benjamingr
approved these changes
Jul 19, 2026
This comment was marked as outdated.
This comment was marked as outdated.
gurgunday
approved these changes
Jul 20, 2026
Collaborator
Speed up
readline.createInterface(), which is on the hot path of every CLI tool that reads lines from a stream.A CPU profile of interface construction showed three avoidable costs:
setupHistoryManager()accounted for ~43% of construction self time: every instance ran fourObjectDefinePropertycalls, allocating six fresh closures and four descriptor objects each time. The accessors (history,historyIndex,historySize,isFlushing) are now defined from a module-level shared descriptor map with a singleObjectDefinePropertiescall. They remain own, enumerable, configurable properties, so observable semantics are unchanged.size,historyandremoveHistoryDuplicatesonto the user-provided input stream to forward them to the history manager. It now passes a plain options object instead, so the user's stream is no longer observably mutated and doesn't go through extra hidden-class transitions. The legacy positional-arguments path is unchanged.Interfacewrapper unconditionally readprocess.env.TERM(which goes through the env interceptor) to bind the dumb-terminal_ttyWrite. The check is now gated onthis.terminal, since_ttyWriteis never invoked when the interface is not in terminal mode.Results with the new benchmark (30 runs, interleaved via
benchmark/compare.js):Update: a second commit speeds up line throughput as well.
[kNormalWrite]ran a regular-expressionexecloop that allocated a match object and two index entries per line, then sliced each line out in JS. It now does a single split pass, and uses a plain string split on"\n"when the chunk contains none of the rare line endings (\r,,), which takes V8's fast string-split path.Results for
readline/readline-iterable.js(20 runs, interleaved):Event-based consumption (
rl.on('line')) goes from 8.1M to 13.3M lines/s (+65%) in a microbenchmark of 32-byte lines.