Skip to content

Commit

Permalink
perf(pkgs-graph): speed up createPkgGraph by using a table for manife…
Browse files Browse the repository at this point in the history
…st name lookup (#6287)
  • Loading branch information
jakebailey committed Mar 27, 2023
1 parent c45a421 commit 35d98c7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-stingrays-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pnpm/workspace.pkgs-graph": patch
---

Speed up createPkgGraph by using a table for manifest name lookup
10 changes: 8 additions & 2 deletions workspace/pkgs-graph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export function createPkgGraph<T> (pkgs: Array<Package & T>, opts?: {
} {
const pkgMap = createPkgMap(pkgs)
const pkgMapValues = Object.values(pkgMap)
const pkgMapByManifestName: Record<string, Package[] | undefined> = {}
for (const pkg of pkgMapValues) {
if (pkg.manifest.name) {
(pkgMapByManifestName[pkg.manifest.name] ??= []).push(pkg)
}
}
const unmatched: Array<{ pkgName: string, range: string }> = []
const graph = mapValues((pkg) => ({
dependencies: createNode(pkg),
Expand Down Expand Up @@ -76,8 +82,8 @@ export function createPkgGraph<T> (pkgs: Array<Package & T>, opts?: {

if (spec.type !== 'version' && spec.type !== 'range') return ''

const pkgs = pkgMapValues.filter(pkg => pkg.manifest.name === depName)
if (pkgs.length === 0) return ''
const pkgs = pkgMapByManifestName[depName]
if (!pkgs || pkgs.length === 0) return ''
const versions = pkgs.filter(({ manifest }) => manifest.version)
.map(pkg => pkg.manifest.version) as string[]

Expand Down

0 comments on commit 35d98c7

Please sign in to comment.