Skip to content

Commit e37e0ca

Browse files
authored
fix: faster paths for compare (#813)
1 parent 2471d75 commit e37e0ca

File tree

5 files changed

+37
-47
lines changed

5 files changed

+37
-47
lines changed

benchmarks/bench-compare.js

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,15 @@ const Benchmark = require('benchmark')
44
const SemVer = require('../classes/semver')
55
const suite = new Benchmark.Suite()
66

7-
const versions = ['1.0.3', '2.2.2', '2.3.0']
8-
const versionToCompare = '1.0.2'
9-
const option1 = { includePrelease: true }
10-
const option2 = { includePrelease: true, loose: true }
11-
const option3 = { includePrelease: true, loose: true, rtl: true }
7+
const comparisons = require('../test/fixtures/comparisons')
128

13-
for (const version of versions) {
14-
suite.add(`compare ${version} to ${versionToCompare}`, function () {
15-
const semver = new SemVer(version)
16-
semver.compare(versionToCompare)
9+
for (const [v0, v1] of comparisons) {
10+
suite.add(`compare ${v0} to ${v1}`, function () {
11+
const semver = new SemVer(v0)
12+
semver.compare(v1)
1713
})
1814
}
1915

20-
for (const version of versions) {
21-
suite.add(
22-
`compare ${version} to ${versionToCompare} with option (${JSON.stringify(option1)})`,
23-
function () {
24-
const semver = new SemVer(version, option1)
25-
semver.compare(versionToCompare)
26-
})
27-
}
28-
29-
for (const version of versions) {
30-
suite.add(`compare ${version} to ${versionToCompare} with option (${JSON.stringify(option2)})`,
31-
function () {
32-
const semver = new SemVer(version, option2)
33-
semver.compare(versionToCompare)
34-
})
35-
}
36-
37-
for (const version of versions) {
38-
suite.add(
39-
`compare ${version} to ${versionToCompare} with option (${JSON.stringify(option3)})`,
40-
function () {
41-
const semver = new SemVer(version, option3)
42-
semver.compare(versionToCompare)
43-
})
44-
}
45-
4616
suite
4717
.on('cycle', function (event) {
4818
console.log(String(event.target))

benchmarks/bench-parse.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22

33
const Benchmark = require('benchmark')
44
const parse = require('../functions/parse')
5-
const { MAX_SAFE_INTEGER } = require('../internal/constants')
65
const suite = new Benchmark.Suite()
76

8-
const cases = ['1.2.1', '1.2.2-4', '1.2.3-pre']
9-
const invalidCases = [`${MAX_SAFE_INTEGER}0.0.0`, 'hello, world', 'xyz']
7+
const cases = require(`../test/fixtures/valid-versions`)
8+
const invalidCases = require(`../test/fixtures/invalid-versions`)
109

1110
for (const test of cases) {
12-
suite.add(`parse(${test})`, function () {
13-
parse(test)
11+
suite.add(`parse(${test[0]})`, function () {
12+
parse(test[0])
1413
})
1514
}
1615

1716
for (const test of invalidCases) {
18-
suite.add(`invalid parse(${test})`, function () {
19-
parse(test)
17+
suite.add(`invalid parse(${test[0]})`, function () {
18+
parse(test[0])
2019
})
2120
}
2221

classes/semver.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,25 @@ class SemVer {
111111
other = new SemVer(other, this.options)
112112
}
113113

114-
return (
115-
compareIdentifiers(this.major, other.major) ||
116-
compareIdentifiers(this.minor, other.minor) ||
117-
compareIdentifiers(this.patch, other.patch)
118-
)
114+
if (this.major < other.major) {
115+
return -1
116+
}
117+
if (this.major > other.major) {
118+
return 1
119+
}
120+
if (this.minor < other.minor) {
121+
return -1
122+
}
123+
if (this.minor > other.minor) {
124+
return 1
125+
}
126+
if (this.patch < other.patch) {
127+
return -1
128+
}
129+
if (this.patch > other.patch) {
130+
return 1
131+
}
132+
return 0
119133
}
120134

121135
comparePre (other) {

internal/identifiers.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
const numeric = /^[0-9]+$/
44
const compareIdentifiers = (a, b) => {
5+
if (typeof a === 'number' && typeof b === 'number') {
6+
return a === b ? 0 : a < b ? -1 : 1
7+
}
8+
59
const anum = numeric.test(a)
610
const bnum = numeric.test(b)
711

test/internal/identifiers.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ test('rcompareIdentifiers and compareIdentifiers', (t) => {
88
['1', '2'],
99
['alpha', 'beta'],
1010
['0', 'beta'],
11+
[1, 2],
1112
]
1213
set.forEach((ab) => {
1314
const a = ab[0]
@@ -17,5 +18,7 @@ test('rcompareIdentifiers and compareIdentifiers', (t) => {
1718
})
1819
t.equal(compareIdentifiers('0', '0'), 0)
1920
t.equal(rcompareIdentifiers('0', '0'), 0)
21+
t.equal(compareIdentifiers(1, 1), 0)
22+
t.equal(rcompareIdentifiers(1, 1), 0)
2023
t.end()
2124
})

0 commit comments

Comments
 (0)