diff --git a/.gitignore b/.gitignore index 1bcc5e25..55fc06f4 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ tap-testdir*/ !/tap-snapshots/ !/test/ !/tsconfig.json +!/benchmarks diff --git a/benchmarks/.gitignore b/benchmarks/.gitignore new file mode 100644 index 00000000..25c8fdba --- /dev/null +++ b/benchmarks/.gitignore @@ -0,0 +1,2 @@ +node_modules +package-lock.json \ No newline at end of file diff --git a/benchmarks/README.md b/benchmarks/README.md new file mode 100644 index 00000000..e69de29b diff --git a/benchmarks/bench-compare.js b/benchmarks/bench-compare.js new file mode 100644 index 00000000..2dfc0900 --- /dev/null +++ b/benchmarks/bench-compare.js @@ -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 }) diff --git a/benchmarks/bench-diff.js b/benchmarks/bench-diff.js new file mode 100644 index 00000000..97d74441 --- /dev/null +++ b/benchmarks/bench-diff.js @@ -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 }) diff --git a/benchmarks/bench-parse-options.js b/benchmarks/bench-parse-options.js new file mode 100644 index 00000000..41ab232c --- /dev/null +++ b/benchmarks/bench-parse-options.js @@ -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 }) diff --git a/benchmarks/bench-parse.js b/benchmarks/bench-parse.js new file mode 100644 index 00000000..e4180c44 --- /dev/null +++ b/benchmarks/bench-parse.js @@ -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 }) diff --git a/benchmarks/bench-satisfies.js b/benchmarks/bench-satisfies.js new file mode 100644 index 00000000..a95a2c30 --- /dev/null +++ b/benchmarks/bench-satisfies.js @@ -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 }) diff --git a/benchmarks/bench-subset.js b/benchmarks/bench-subset.js new file mode 100644 index 00000000..f8825fae --- /dev/null +++ b/benchmarks/bench-subset.js @@ -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 }) diff --git a/benchmarks/package.json b/benchmarks/package.json new file mode 100644 index 00000000..77d7aee3 --- /dev/null +++ b/benchmarks/package.json @@ -0,0 +1,13 @@ +{ + "name": "node-semver-benchmarks", + "version": "1.0.0", + "description": "", + "main": "", + "scripts": { + + }, + "dependencies": { + "benchmark": "2.1.4" + }, + "license": "ISC" +}