Skip to content

Commit

Permalink
chore: add benchmarks (#696)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
H4ad authored Apr 7, 2024
1 parent 692451b commit b236c3d
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ tap-testdir*/
!/.gitignore
!/.npmrc
!/.release-please-manifest.json
!/benchmarks
!/bin/
!/CHANGELOG*
!/classes/
Expand Down
48 changes: 48 additions & 0 deletions benchmarks/bench-compare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const Benchmark = require('benchmark')
const SemVer = require('../classes/semver')
const suite = new Benchmark.Suite()

const versions = ['1.0.3', '2.2.2', '2.3.0']
const versionToCompare = '1.0.2'
const option1 = { includePrelease: true }
const option2 = { includePrelease: true, loose: true }
const option3 = { includePrelease: true, loose: true, rtl: true }

for (const version of versions) {
suite.add(`compare ${version} to ${versionToCompare}`, function () {
const semver = new SemVer(version)
semver.compare(versionToCompare)
})
}

for (const version of versions) {
suite.add(
`compare ${version} to ${versionToCompare} with option (${JSON.stringify(option1)})`,
function () {
const semver = new SemVer(version, option1)
semver.compare(versionToCompare)
})
}

for (const version of versions) {
suite.add(`compare ${version} to ${versionToCompare} with option (${JSON.stringify(option2)})`,
function () {
const semver = new SemVer(version, option2)
semver.compare(versionToCompare)
})
}

for (const version of versions) {
suite.add(
`compare ${version} to ${versionToCompare} with option (${JSON.stringify(option3)})`,
function () {
const semver = new SemVer(version, option3)
semver.compare(versionToCompare)
})
}

suite
.on('cycle', function (event) {
console.log(String(event.target))
})
.run({ async: false })
21 changes: 21 additions & 0 deletions benchmarks/bench-diff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const Benchmark = require('benchmark')
const diff = require('../functions/diff')
const suite = new Benchmark.Suite()

const cases = [
['0.0.1', '0.0.1-pre', 'patch'],
['0.0.1', '0.0.1-pre-2', 'patch'],
['1.1.0', '1.1.0-pre', 'minor'],
]

for (const [v1, v2] of cases) {
suite.add(`diff(${v1}, ${v2})`, function () {
diff(v1, v2)
})
}

suite
.on('cycle', function (event) {
console.log(String(event.target))
})
.run({ async: false })
33 changes: 33 additions & 0 deletions benchmarks/bench-parse-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const Benchmark = require('benchmark')
const parseOptions = require('../internal/parse-options')
const suite = new Benchmark.Suite()

const options1 = {
includePrerelease: true,
}

const options2 = {
includePrerelease: true,
loose: true,
}

const options3 = {
includePrerelease: true,
loose: true,
rtl: false,
}

suite
.add('includePrerelease', function () {
parseOptions(options1)
})
.add('includePrerelease + loose', function () {
parseOptions(options2)
})
.add('includePrerelease + loose + rtl', function () {
parseOptions(options3)
})
.on('cycle', function (event) {
console.log(String(event.target))
})
.run({ async: false })
25 changes: 25 additions & 0 deletions benchmarks/bench-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const Benchmark = require('benchmark')
const parse = require('../functions/parse')
const { MAX_SAFE_INTEGER } = require('../internal/constants')
const suite = new Benchmark.Suite()

const cases = ['1.2.1', '1.2.2-4', '1.2.3-pre']
const invalidCases = [`${MAX_SAFE_INTEGER}0.0.0`, 'hello, world', 'xyz']

for (const test of cases) {
suite.add(`parse(${test})`, function () {
parse(test)
})
}

for (const test of invalidCases) {
suite.add(`invalid parse(${test})`, function () {
parse(test)
})
}

suite
.on('cycle', function (event) {
console.log(String(event.target))
})
.run({ async: false })
39 changes: 39 additions & 0 deletions benchmarks/bench-satisfies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const Benchmark = require('benchmark')
const satisfies = require('../functions/satisfies')
const suite = new Benchmark.Suite()

const versions = ['1.0.3||^2.0.0', '2.2.2||~3.0.0', '2.3.0||<4.0.0']
const versionToCompare = '1.0.6'
const option1 = { includePrelease: true }
const option2 = { includePrelease: true, loose: true }
const option3 = { includePrelease: true, loose: true, rtl: true }

for (const version of versions) {
suite.add(`satisfies(${versionToCompare}, ${version})`, function () {
satisfies(versionToCompare, version)
})
}

for (const version of versions) {
suite.add(`satisfies(${versionToCompare}, ${version}, ${JSON.stringify(option1)})`, function () {
satisfies(versionToCompare, version, option1)
})
}

for (const version of versions) {
suite.add(`satisfies(${versionToCompare}, ${version}, ${JSON.stringify(option2)})`, function () {
satisfies(versionToCompare, version, option2)
})
}

for (const version of versions) {
suite.add(`satisfies(${versionToCompare}, ${version}, ${JSON.stringify(option3)})`, function () {
satisfies(versionToCompare, version, option3)
})
}

suite
.on('cycle', function (event) {
console.log(String(event.target))
})
.run({ async: false })
25 changes: 25 additions & 0 deletions benchmarks/bench-subset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const Benchmark = require('benchmark')
const subset = require('../ranges/subset')
const suite = new Benchmark.Suite()

// taken from tests
const cases = [
// everything is a subset of *
['1.2.3', '*', true],
['^1.2.3', '*', true],
['^1.2.3-pre.0', '*', false],
['^1.2.3-pre.0', '*', true, { includePrerelease: true }],
['1 || 2 || 3', '*', true],
]

for (const [sub, dom] of cases) {
suite.add(`subset(${sub}, ${dom})`, function () {
subset(sub, dom)
})
}

suite
.on('cycle', function (event) {
console.log(String(event.target))
})
.run({ async: false })
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"devDependencies": {
"@npmcli/eslint-config": "^4.0.0",
"@npmcli/template-oss": "4.21.3",
"benchmark": "^2.1.4",
"tap": "^16.0.0"
},
"license": "ISC",
Expand Down Expand Up @@ -71,7 +72,8 @@
"/ranges/",
"/index.js",
"/preload.js",
"/range.bnf"
"/range.bnf",
"/benchmarks"
],
"publish": "true"
}
Expand Down

0 comments on commit b236c3d

Please sign in to comment.