Skip to content

Commit

Permalink
chore: add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Apr 7, 2024
1 parent 692451b commit 7eb82a5
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ tap-testdir*/
!/tap-snapshots/
!/test/
!/tsconfig.json
!/benchmarks
2 changes: 2 additions & 0 deletions benchmarks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
Empty file added benchmarks/README.md
Empty file.
43 changes: 43 additions & 0 deletions benchmarks/bench-compare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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 () {

Check failure on line 19 in benchmarks/bench-compare.js

View workflow job for this annotation

GitHub Actions / Lint

This line has a length of 111. Maximum allowed is 100
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 () {

Check failure on line 26 in benchmarks/bench-compare.js

View workflow job for this annotation

GitHub Actions / Lint

This line has a length of 111. Maximum allowed is 100
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 () {

Check failure on line 33 in benchmarks/bench-compare.js

View workflow job for this annotation

GitHub Actions / Lint

This line has a length of 111. Maximum allowed is 100
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');

Check failure on line 1 in benchmarks/bench-parse.js

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
const parse = require('../functions/parse');

Check failure on line 2 in benchmarks/bench-parse.js

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
const { MAX_SAFE_INTEGER } = require('../internal/constants');

Check failure on line 3 in benchmarks/bench-parse.js

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
const suite = new Benchmark.Suite();

Check failure on line 4 in benchmarks/bench-parse.js

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon

const cases = ['1.2.1', '1.2.2-4', '1.2.3-pre'];

Check failure on line 6 in benchmarks/bench-parse.js

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
const invalidCases = [`${MAX_SAFE_INTEGER}0.0.0`, 'hello, world', 'xyz']

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

Check failure on line 11 in benchmarks/bench-parse.js

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
});

Check failure on line 12 in benchmarks/bench-parse.js

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
}

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 })
13 changes: 13 additions & 0 deletions benchmarks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "node-semver-benchmarks",
"version": "1.0.0",
"description": "",
"main": "",
"scripts": {

},
"dependencies": {
"benchmark": "2.1.4"
},
"license": "ISC"
}

0 comments on commit 7eb82a5

Please sign in to comment.