Skip to content

Commit b236c3d

Browse files
authoredApr 7, 2024··
chore: add benchmarks (#696)
I had these benchmarks from the last time I did some perf research in this repo, so I thought maybe it was a good idea to have them in the repo itself, so other people can use them and you guys can verify if the perf is still good or you had any regression.
1 parent 692451b commit b236c3d

8 files changed

+195
-1
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ tap-testdir*/
1414
!/.gitignore
1515
!/.npmrc
1616
!/.release-please-manifest.json
17+
!/benchmarks
1718
!/bin/
1819
!/CHANGELOG*
1920
!/classes/

‎benchmarks/bench-compare.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const Benchmark = require('benchmark')
2+
const SemVer = require('../classes/semver')
3+
const suite = new Benchmark.Suite()
4+
5+
const versions = ['1.0.3', '2.2.2', '2.3.0']
6+
const versionToCompare = '1.0.2'
7+
const option1 = { includePrelease: true }
8+
const option2 = { includePrelease: true, loose: true }
9+
const option3 = { includePrelease: true, loose: true, rtl: true }
10+
11+
for (const version of versions) {
12+
suite.add(`compare ${version} to ${versionToCompare}`, function () {
13+
const semver = new SemVer(version)
14+
semver.compare(versionToCompare)
15+
})
16+
}
17+
18+
for (const version of versions) {
19+
suite.add(
20+
`compare ${version} to ${versionToCompare} with option (${JSON.stringify(option1)})`,
21+
function () {
22+
const semver = new SemVer(version, option1)
23+
semver.compare(versionToCompare)
24+
})
25+
}
26+
27+
for (const version of versions) {
28+
suite.add(`compare ${version} to ${versionToCompare} with option (${JSON.stringify(option2)})`,
29+
function () {
30+
const semver = new SemVer(version, option2)
31+
semver.compare(versionToCompare)
32+
})
33+
}
34+
35+
for (const version of versions) {
36+
suite.add(
37+
`compare ${version} to ${versionToCompare} with option (${JSON.stringify(option3)})`,
38+
function () {
39+
const semver = new SemVer(version, option3)
40+
semver.compare(versionToCompare)
41+
})
42+
}
43+
44+
suite
45+
.on('cycle', function (event) {
46+
console.log(String(event.target))
47+
})
48+
.run({ async: false })

‎benchmarks/bench-diff.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const Benchmark = require('benchmark')
2+
const diff = require('../functions/diff')
3+
const suite = new Benchmark.Suite()
4+
5+
const cases = [
6+
['0.0.1', '0.0.1-pre', 'patch'],
7+
['0.0.1', '0.0.1-pre-2', 'patch'],
8+
['1.1.0', '1.1.0-pre', 'minor'],
9+
]
10+
11+
for (const [v1, v2] of cases) {
12+
suite.add(`diff(${v1}, ${v2})`, function () {
13+
diff(v1, v2)
14+
})
15+
}
16+
17+
suite
18+
.on('cycle', function (event) {
19+
console.log(String(event.target))
20+
})
21+
.run({ async: false })

‎benchmarks/bench-parse-options.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const Benchmark = require('benchmark')
2+
const parseOptions = require('../internal/parse-options')
3+
const suite = new Benchmark.Suite()
4+
5+
const options1 = {
6+
includePrerelease: true,
7+
}
8+
9+
const options2 = {
10+
includePrerelease: true,
11+
loose: true,
12+
}
13+
14+
const options3 = {
15+
includePrerelease: true,
16+
loose: true,
17+
rtl: false,
18+
}
19+
20+
suite
21+
.add('includePrerelease', function () {
22+
parseOptions(options1)
23+
})
24+
.add('includePrerelease + loose', function () {
25+
parseOptions(options2)
26+
})
27+
.add('includePrerelease + loose + rtl', function () {
28+
parseOptions(options3)
29+
})
30+
.on('cycle', function (event) {
31+
console.log(String(event.target))
32+
})
33+
.run({ async: false })

‎benchmarks/bench-parse.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const Benchmark = require('benchmark')
2+
const parse = require('../functions/parse')
3+
const { MAX_SAFE_INTEGER } = require('../internal/constants')
4+
const suite = new Benchmark.Suite()
5+
6+
const cases = ['1.2.1', '1.2.2-4', '1.2.3-pre']
7+
const invalidCases = [`${MAX_SAFE_INTEGER}0.0.0`, 'hello, world', 'xyz']
8+
9+
for (const test of cases) {
10+
suite.add(`parse(${test})`, function () {
11+
parse(test)
12+
})
13+
}
14+
15+
for (const test of invalidCases) {
16+
suite.add(`invalid parse(${test})`, function () {
17+
parse(test)
18+
})
19+
}
20+
21+
suite
22+
.on('cycle', function (event) {
23+
console.log(String(event.target))
24+
})
25+
.run({ async: false })

‎benchmarks/bench-satisfies.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const Benchmark = require('benchmark')
2+
const satisfies = require('../functions/satisfies')
3+
const suite = new Benchmark.Suite()
4+
5+
const versions = ['1.0.3||^2.0.0', '2.2.2||~3.0.0', '2.3.0||<4.0.0']
6+
const versionToCompare = '1.0.6'
7+
const option1 = { includePrelease: true }
8+
const option2 = { includePrelease: true, loose: true }
9+
const option3 = { includePrelease: true, loose: true, rtl: true }
10+
11+
for (const version of versions) {
12+
suite.add(`satisfies(${versionToCompare}, ${version})`, function () {
13+
satisfies(versionToCompare, version)
14+
})
15+
}
16+
17+
for (const version of versions) {
18+
suite.add(`satisfies(${versionToCompare}, ${version}, ${JSON.stringify(option1)})`, function () {
19+
satisfies(versionToCompare, version, option1)
20+
})
21+
}
22+
23+
for (const version of versions) {
24+
suite.add(`satisfies(${versionToCompare}, ${version}, ${JSON.stringify(option2)})`, function () {
25+
satisfies(versionToCompare, version, option2)
26+
})
27+
}
28+
29+
for (const version of versions) {
30+
suite.add(`satisfies(${versionToCompare}, ${version}, ${JSON.stringify(option3)})`, function () {
31+
satisfies(versionToCompare, version, option3)
32+
})
33+
}
34+
35+
suite
36+
.on('cycle', function (event) {
37+
console.log(String(event.target))
38+
})
39+
.run({ async: false })

‎benchmarks/bench-subset.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const Benchmark = require('benchmark')
2+
const subset = require('../ranges/subset')
3+
const suite = new Benchmark.Suite()
4+
5+
// taken from tests
6+
const cases = [
7+
// everything is a subset of *
8+
['1.2.3', '*', true],
9+
['^1.2.3', '*', true],
10+
['^1.2.3-pre.0', '*', false],
11+
['^1.2.3-pre.0', '*', true, { includePrerelease: true }],
12+
['1 || 2 || 3', '*', true],
13+
]
14+
15+
for (const [sub, dom] of cases) {
16+
suite.add(`subset(${sub}, ${dom})`, function () {
17+
subset(sub, dom)
18+
})
19+
}
20+
21+
suite
22+
.on('cycle', function (event) {
23+
console.log(String(event.target))
24+
})
25+
.run({ async: false })

‎package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"devDependencies": {
1616
"@npmcli/eslint-config": "^4.0.0",
1717
"@npmcli/template-oss": "4.21.3",
18+
"benchmark": "^2.1.4",
1819
"tap": "^16.0.0"
1920
},
2021
"license": "ISC",
@@ -71,7 +72,8 @@
7172
"/ranges/",
7273
"/index.js",
7374
"/preload.js",
74-
"/range.bnf"
75+
"/range.bnf",
76+
"/benchmarks"
7577
],
7678
"publish": "true"
7779
}

0 commit comments

Comments
 (0)
Please sign in to comment.