Skip to content

perf: defer source map generation until first access#2962

Closed
MathiasWP wants to merge 3 commits intosveltejs:masterfrom
MathiasWP:perf/lazy-source-map-generation
Closed

perf: defer source map generation until first access#2962
MathiasWP wants to merge 3 commits intosveltejs:masterfrom
MathiasWP:perf/lazy-source-map-generation

Conversation

@MathiasWP
Copy link
Contributor

Summary

  • generateMap({ hires: true }) accounts for ~10% of svelte2tsx wall-clock time
  • Most consumers never need the source map on the hot path:
    • Language server: getText(), getLength(), getChangeRange() don't use the map. The mapper is already lazily initialized, but the map object itself was eagerly computed.
    • svelte-check incremental mode: only uses .code, never .map
  • The map property is now a lazy getter that calls generateMap() on first access and caches the result via Object.defineProperty
  • Consumers that do access .map (TypeScript plugin, sourcemap tests) see no regression — the map is generated once on first access

Benchmarks

Measured with realistic generated Svelte components of various sizes:

Component size Without .map access With .map access Savings
Small (30 lines) 0.61ms 0.60ms ~0% (map gen is trivial)
Medium (100 lines) 1.50ms 1.82ms 17%
Large (300 lines) 4.50ms 5.02ms 10%
XL (600 lines) 8.93ms 9.98ms 11%

Test plan

  • All 365 svelte2tsx tests pass (including 16 sourcemap tests)
  • All 665 language-server tests pass

🤖 Generated with Claude Code

generateMap({ hires: true }) accounts for ~10% of svelte2tsx time.
Most consumers (language server getText/getLength/getChangeRange,
svelte-check incremental mode) never need the source map, so deferring
generation to a lazy getter avoids this cost on the hot path.

The map is cached on first access via Object.defineProperty so
subsequent reads don't regenerate.

Benchmarks (300-line component):
- Without .map access: 4.5ms (10% faster)
- With .map access: 5.0ms (unchanged)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@changeset-bot
Copy link

changeset-bot bot commented Feb 27, 2026

🦋 Changeset detected

Latest commit: a1b3c06

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
svelte2tsx Patch
svelte-language-server Patch
typescript-svelte-plugin Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

MathiasWP and others added 2 commits February 27, 2026 10:58
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@jasonlyu123
Copy link
Member

This won't make any difference. The map property is accessed immediately.

@MathiasWP MathiasWP closed this Mar 2, 2026
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