Skip to content

Bind node:console's _stdout and _stderr lazily - #8777

Merged
littledivy merged 1 commit into
mainfrom
littledivy/2026-11-21-startup-console-lazy
Nov 24, 2026
Merged

littledivy merged 1 commit into
mainfrom
littledivy/2026-11-21-startup-console-lazy

Conversation

@littledivy

@littledivy littledivy commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

process.stdout and process.stderr are accessors that load the stdio streams, and with them the fs and streams modules, on first access. node:console read both at the top level to set console._stdout and console._stderr, so importing it paid for that whole module graph.

In this patch _stdout and _stderr are accessors that read the process stream on first access and keep it, as Node.js does for its global console. Assigning one replaces the stream, as before. A Worker that reads one pays the same as before, at that point instead of at import. A new case in console-nodejs-test.js checks both.

Cost over an empty worker Before After Change
import 'node:console', first isolate 5.91 ms 1.58 ms 3.7x
import 'node:console', each extra isolate 4.75 ms 1.33 ms 3.6x
import + read console._stdout, first isolate 6.10 ms 5.53 ms -9.3% (noise)
import + read console._stdout, each extra isolate 4.81 ms 4.96 ms +3.1% (noise)

Medians of 150 interleaved runs of workerd test with 1 and 9 services, -c opt, Linux x86-64.

@littledivy
littledivy requested review from a team as code owners November 21, 2026 05:36

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of bc24945f.

This review, of 3f3c53f8

✅ Approved — A small, self-contained change that keeps the old property semantics and is covered by a new test; no issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

The lazy _stdout and _stderr accessors keep the old property semantics: they are configurable and enumerable, and assigning one replaces the stream. They follow the descriptor pattern already used for process.stdout and process.stderr. Every read in internal_console.ts goes through the property. The new test checks caching, that each accessor matches the process stream, and reassignment, and it restores the original streams afterwards.


📝 Documentation: ✅ LGTM

The new code comment describes how the code behaves now and why, as the repository's comment guidelines ask.


Reviewed commit: 3f3c53f8

process.stdout and process.stderr are accessors that load the stdio
streams, and with them the fs and streams modules, on first access.
node:console read both at the top level to set console._stdout and
console._stderr, so importing it paid for that whole module graph.

In this patch _stdout and _stderr are accessors that read the process
stream on first access and keep it, as Node.js does for its global
console. Assigning one replaces the stream, as before. A Worker that
reads one pays the same as before, at that point instead of at import.
Cost over an empty worker, medians of 150 interleaved runs:

                                    before    after
  import, first isolate            5.91 ms  1.58 ms  (3.7x)
  import, extra isolate            4.75 ms  1.33 ms  (3.6x)
  import + _stdout, first isolate  6.10 ms  5.53 ms  (-9.3%, noise)
  import + _stdout, extra isolate  4.81 ms  4.96 ms  (+3.1%, noise)
@littledivy
littledivy force-pushed the littledivy/2026-11-21-startup-console-lazy branch from 3f3c53f to bc24945 Compare November 21, 2026 06:00

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — A small, self-contained change that keeps the old property semantics and is covered by a new test; no issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

The lazy _stdout and _stderr accessors keep the old property semantics: they are configurable and enumerable, and assigning one replaces the stream. They follow the descriptor pattern already used for process.stdout and process.stderr. Every read in internal_console.ts goes through the property. The new test checks caching, that each accessor matches the process stream, and reassignment, and it restores the original streams afterwards.


📝 Documentation: ✅ LGTM

The new code comment describes how the code behaves now and why, as the repository's comment guidelines ask.


Reviewed commit: bc24945f — same changes as 3f3c53f8, so this is that commit's review (no new review was run; line numbers are as of 3f3c53f8)

@littledivy
littledivy merged commit 6bc2cc6 into main Nov 24, 2026
23 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-21-startup-console-lazy branch November 24, 2026 02:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants