Skip to content

Short-circuit split/join for single-line messages in formatArgs #6

@alanzabihi

Description

@alanzabihi

Hypothesis

The split/join on node.js line 175 (args[0].split('\n').join('\n' + prefix)) runs on every enabled call with colors, but most debug messages are single-line. Checking for '\n' before splitting avoids creating intermediate arrays for the common case.

Rationale

split('\n').join() creates an array, iterates it, and builds a new string. For single-line messages (no '\n'), the input is returned unchanged after unnecessary allocation. An indexOf('\n') check (or equivalent) skips this entirely for ~75% of benchmark calls (3 of 4 call types are single-line).

Scope

  • src/node.js formatArgs() line 175: add indexOf('\n') guard before split/join

Expected impact

Small to moderate. Eliminates array allocation for most calls. The split/join is only expensive for multiline messages which are the minority.

Risks

None. The check is a strict fast-path — behavior is identical when '\n' is present.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions