Skip to content

Commit 70497cb

Browse files
authored
fix(perf): avoid importing the entire semver package for update-notifier (#7346)
There are a bunch of places were we load `semver`, I'm trying to see if I can remove the full import for `semver` and only import the specific functions. Currently, I didn't have any perf improvement since we still load the entire `semver`, once we have removed all the package loads, then we could see some improvement (a little bit).
1 parent 90ba1c9 commit 70497cb

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lib/utils/update-notifier.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
// Check daily for betas, and weekly otherwise.
44

55
const ciInfo = require('ci-info')
6-
const semver = require('semver')
6+
const gt = require('semver/functions/gt')
7+
const gte = require('semver/functions/gte')
8+
const parse = require('semver/functions/parse')
79
const { stat, writeFile } = require('fs/promises')
810
const { resolve } = require('path')
911

@@ -38,12 +40,12 @@ const updateCheck = async (npm, spec, version, current) => {
3840
// and should get the updates from that release train.
3941
// Note that this isn't another http request over the network, because
4042
// the packument will be cached by pacote from previous request.
41-
if (semver.gt(version, latest) && spec === 'latest') {
43+
if (gt(version, latest) && spec === 'latest') {
4244
return updateNotifier(npm, `^${version}`)
4345
}
4446

4547
// if we already have something >= the desired spec, then we're done
46-
if (semver.gte(version, latest)) {
48+
if (gte(version, latest)) {
4749
return null
4850
}
4951

@@ -53,7 +55,7 @@ const updateCheck = async (npm, spec, version, current) => {
5355
// ok! notify the user about this update they should get.
5456
// The message is saved for printing at process exit so it will not get
5557
// lost in any other messages being printed as part of the command.
56-
const update = semver.parse(mani.version)
58+
const update = parse(mani.version)
5759
const type = update.major !== current.major ? 'major'
5860
: update.minor !== current.minor ? 'minor'
5961
: update.patch !== current.patch ? 'patch'
@@ -79,7 +81,7 @@ const updateNotifier = async (npm, spec = 'latest') => {
7981
// if we're on a prerelease train, then updates are coming fast
8082
// check for a new one daily. otherwise, weekly.
8183
const { version } = npm
82-
const current = semver.parse(version)
84+
const current = parse(version)
8385

8486
// if we're on a beta train, always get the next beta
8587
if (current.prerelease.length) {

0 commit comments

Comments
 (0)