Skip to content

Combined: cache color prefix, short-circuit newline split, and cache humanize #13

@alanzabihi

Description

@alanzabihi

Hypothesis

Combining three independent optimizations in formatArgs can achieve a compound improvement that individually each falls below the 3ms tolerance but together exceeds it:

  1. Pre-compute ANSI color prefix at init (thesis Pre-compute ANSI color prefix per instance at init time #5 direction)
  2. Short-circuit split/join when no newline present (thesis Short-circuit split/join for single-line messages in formatArgs #6 direction)
  3. Cache ms.humanize result for repeated identical diffs (thesis Cache ms.humanize result for identical consecutive diffs #12 direction)

Rationale

Prior theses #5 and #6 were each decided as non_improvement individually, likely because each saved only 1-2ms on the current baseline. But formatArgs runs on every enabled call and these three optimizations target independent bottlenecks within it:

  • Prefix computation: eliminates per-call string concatenation for ANSI codes
  • Newline check: avoids split/join array allocation for 75% of calls
  • Humanize cache: avoids ms library call when diff repeats

Combined, these should produce a measurable improvement.

Scope

  • src/node.js init(): cache _colorCode and _colorPrefix on the instance
  • src/node.js formatArgs(): use cached prefix, add indexOf newline guard, cache humanize result

Expected impact

Moderate to high. Each optimization independently saves ~1-2ms; combined they should exceed the 3ms tolerance.

Risks

Must verify the combination does not interact negatively. Each optimization is independent (different lines of formatArgs) so interference is unlikely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions