Skip to content

Commit a67317b

Browse files
authored
fix(perf): lazy load hosted git info on normalize (#90)
Combined with #89, we can reduce the load time of `normalize` to less than `1ms`, making the `run-script` load in `4ms` instead of `13ms`.
1 parent 06df698 commit a67317b

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

lib/normalize.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ const clean = require('semver/functions/clean')
33
const fs = require('fs/promises')
44
const path = require('path')
55
const log = require('proc-log')
6-
const hostedGitInfo = require('hosted-git-info')
6+
7+
/**
8+
* @type {import('hosted-git-info')}
9+
*/
10+
let _hostedGitInfo
11+
function lazyHostedGitInfo () {
12+
if (!_hostedGitInfo) {
13+
_hostedGitInfo = require('hosted-git-info')
14+
}
15+
return _hostedGitInfo
16+
}
717

818
/**
919
* @type {import('glob').glob}
@@ -459,7 +469,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
459469
}
460470
}
461471
if (data.repository.url) {
462-
const hosted = hostedGitInfo.fromUrl(data.repository.url)
472+
const hosted = lazyHostedGitInfo().fromUrl(data.repository.url)
463473
let r
464474
if (hosted) {
465475
if (hosted.getDefaultRepresentation() === 'shortcut') {
@@ -519,7 +529,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
519529
changes?.push(`Removed invalid "${deps}.${d}"`)
520530
delete data[deps][d]
521531
}
522-
const hosted = hostedGitInfo.fromUrl(data[deps][d])?.toString()
532+
const hosted = lazyHostedGitInfo().fromUrl(data[deps][d])?.toString()
523533
if (hosted && hosted !== data[deps][d]) {
524534
changes?.push(`Normalized git reference to "${deps}.${d}"`)
525535
data[deps][d] = hosted.toString()

0 commit comments

Comments
 (0)