Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(read-project-manifest): optimize normalize function #6763

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hot-cobras-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pnpm/read-project-manifest": patch
---

Optimizing project manifest normalization, reducing amoung of data copying
2 changes: 2 additions & 0 deletions pkg-manifest/read-project-manifest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"fast-deep-equal": "^3.1.3",
"is-windows": "^1.0.2",
"json5": "^2.2.3",
"lodash.clonedeep": "^4.5.0",
"parse-json": "^5.2.0",
"read-yaml-file": "^2.1.0",
"sort-keys": "^4.2.0",
Expand All @@ -46,6 +47,7 @@
"devDependencies": {
"@pnpm/read-project-manifest": "workspace:*",
"@types/is-windows": "^1.0.0",
"@types/lodash.clonedeep": "^4.5.7",
"@types/parse-json": "^4.0.0",
"tempy": "^1.0.1"
},
Expand Down
28 changes: 19 additions & 9 deletions pkg-manifest/read-project-manifest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import readYamlFile from 'read-yaml-file'
import detectIndent from '@gwhitney/detect-indent'
import equal from 'fast-deep-equal'
import isWindows from 'is-windows'
import sortKeys from 'sort-keys'
import cloneDeep from 'lodash.clonedeep'
import {
readJson5File,
readJsonFile,
Expand Down Expand Up @@ -214,14 +214,24 @@ const dependencyKeys = new Set([
])

function normalize (manifest: ProjectManifest) {
manifest = JSON.parse(JSON.stringify(manifest))
const result: Record<string, any> = {} // eslint-disable-line @typescript-eslint/no-explicit-any

for (const [key, value] of Object.entries(manifest)) {
if (!dependencyKeys.has(key)) {
result[key] = value
} else if (Object.keys(value).length !== 0) {
result[key] = sortKeys(value)
const result: Record<string, unknown> = {} // eslint-disable-line @typescript-eslint/no-explicit-any
for (const key in manifest) {
if (Object.prototype.hasOwnProperty.call(manifest, key)) {
const value = manifest[key as keyof ProjectManifest]
if (typeof value !== 'object' || !dependencyKeys.has(key)) {
result[key] = cloneDeep(value)
} else {
const keys = Object.keys(value)
if (keys.length !== 0) {
keys.sort()
const sortedValue: Record<string, unknown> = {}
for (const k of keys) {
// @ts-expect-error this is fine
sortedValue[k] = value[k]
}
result[key] = sortedValue
}
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.