Skip to content

perf(core): cache TraceMap and parsed sourcemaps, use Set for coverage file lookup#1074

Merged
fi3ework merged 2 commits intomainfrom
perf-souremap
Mar 19, 2026
Merged

perf(core): cache TraceMap and parsed sourcemaps, use Set for coverage file lookup#1074
fi3ework merged 2 commits intomainfrom
perf-souremap

Conversation

@fi3ework
Copy link
Copy Markdown
Member

@fi3ework fi3ework commented Mar 19, 2026

Summary

Add caching layers in the sourcemap resolution and error stack parsing paths
to eliminate redundant work when the same file is referenced by multiple
stack frames or errors within a single test run.

Not on the hot path today, but they are zero-cost correctness improvements:

  • Function-scoped Map, created and discarded each call — no stale state, no watch-mode risk
  • Eliminates redundant new TraceMap() (VLQ decode) and JSON.parse() when multiple stack frames reference the same file
  • The effect is visible when a run produces many failures pointing to the same source files (e.g., a broken import causing 100+ test failures)
  • Even when the savings are small, caching a pure lookup is the idiomatic pattern — it makes the intent clear and prevents future regressions if this path becomes hotter

Changes:

  1. Cache TraceMap per file in both parseErrorStacktrace implementations
    (packages/core/src/utils/error.ts and packages/core/src/reporter/md.ts).
    Avoids redundant VLQ decoding when multiple stack frames originate from the
    same source file.

  2. Memoize JSON.parse in getSourcemap (packages/core/src/core/runTests.ts).
    A per-run parsedSourceMapCache prevents repeated parsing of the same
    sourcemap JSON when the same file appears in multiple errors.

  3. Use Set.has instead of Array.includes for coverage file lookup
    (packages/core/src/coverage/generate.ts). Converts O(n) linear scans to
    O(1) hash lookups when filtering covered/uncovered files.

  4. Fix fullStack condition in md.ts reporter (packages/core/src/reporter/md.ts).
    Only re-parse the full stack when error.fullStack is truthy, avoiding
    unnecessary duplicate parsing when the full stack is not requested.

All caches are scoped to per-call or per-run() lifetime, so they are
automatically invalidated on each watch-mode rerun.

Related Links

  • Node perf optimization skill: .agents/skills/node-perf-optimization/

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@fi3ework fi3ework requested a review from 9aoy March 19, 2026 08:06
@fi3ework fi3ework requested a review from 9aoy March 19, 2026 11:13
@fi3ework fi3ework merged commit c2478e0 into main Mar 19, 2026
16 checks passed
@fi3ework fi3ework deleted the perf-souremap branch March 19, 2026 11:33
@9aoy 9aoy mentioned this pull request Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants