Skip to content

Pre-compute ANSI color prefix per instance at init time #5

@alanzabihi

Description

@alanzabihi

Hypothesis

The ANSI color prefix string in formatArgs (node.js lines 171-175) is recomputed on every enabled call, but depends only on this.color and this.namespace which are fixed at instance creation. Caching it at init time eliminates per-call string concatenation.

Rationale

Every enabled call rebuilds:

  • colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c)
  • prefix = ' ${colorCode};1m${name} \u001B[0m'

These are pure functions of color and namespace. Computing once at init and storing as debug._colorPrefix saves 2-3 string concatenations per enabled call (200K times in the benchmark).

Scope

  • src/node.js init() (lines 231-238): compute and store prefix string on debug instance
  • src/node.js formatArgs() (lines 167-180): use cached prefix instead of recomputing

Expected impact

Moderate. Eliminates per-call string alloc for the prefix. The suffix still varies by diff so it cannot be cached.

Risks

Low. The color and namespace are immutable once the debug instance is created.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions