perf: micro-optimizations in hot paths#2960
Merged
jasonlyu123 merged 6 commits intosveltejs:masterfrom Mar 14, 2026
Merged
Conversation
…gnostics Replace O(n²) flatten() with Array.prototype.flat(), remove lodash flatten dependency from PluginHost, use Object.create instead of object spread in svelteNodeAt AST walking, cache getExistingImports per document, use RegExp.test() over .match(), replace regex with charCode check in possiblyComponent, use cached line offsets for lineCount, and optimize iteration in SnapshotManager. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: eb6b785 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
jasonlyu123
requested changes
Mar 2, 2026
packages/language-server/src/plugins/typescript/SnapshotManager.ts
Outdated
Show resolved
Hide resolved
packages/language-server/src/plugins/typescript/features/CompletionProvider.ts
Show resolved
Hide resolved
- Replace all flatten() call sites with .flat()/.flatMap() and remove the flatten utility function entirely - Use charCodeAt(0) directly in possiblyComponent instead of char comparison - Use block syntax for single-line for loops in SnapshotManager - Remove ineffective existingImportsCache from CompletionProvider Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
jasonlyu123
approved these changes
Mar 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
flatten()withArray.prototype.flat()— the existing reduce+spread implementation copies the accumulator on every iteration. Called from DiagnosticsProvider, CompletionProvider, FindReferencesProvider, CodeActionsProvider, and moreflattenfrom PluginHost — replaces ~10flatten(...)call sites with.flat(). PluginHost is the central dispatch for all LSP operationsObject.create(node)instead of{ ...node }insvelteNodeAt— avoids copying all AST node properties on every matching ancestor while walking the tree. Called from completions, diagnostics, rename, and semantic tokensgetExistingImportsper document URI + version — avoids re-parsing all imports via regex on every completion requestRegExp.test()inisExistingSvelteComponentImport— avoids allocating a match array for every Svelte component completion itempossiblyComponentregex with charCode comparison — regex is overkill for checking a single ASCII uppercase characterlineCountto use cached line offsets — was splitting the entire document text on every call whengetLineOffsets()is already cachedgetByPrefixto iterate without intermediate array — push loop instead ofArray.from+ filter + map chainlogStatisticsto single-pass counting — replaces Set→Array conversion and double.filter()with a single loopTest plan
pnpm build🤖 Generated with Claude Code