From 528a6a0b116a2af876b174c019617750cef6ff2a Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Tue, 3 Sep 2024 11:37:59 -0300 Subject: [PATCH 1/4] benchmark: add strictEqual and notStrictEqual bench --- benchmark/assert/strictequal.js | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 benchmark/assert/strictequal.js diff --git a/benchmark/assert/strictequal.js b/benchmark/assert/strictequal.js new file mode 100644 index 00000000000000..21a77f0472c5fc --- /dev/null +++ b/benchmark/assert/strictequal.js @@ -0,0 +1,43 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + n: [25, 2e5], + type: ['string', 'object', 'number'], + method: ['strictEqual', 'notStrictEqual'], +}); + +function main({ type, n, method }) { + const fn = assert[method]; + let actual, expected; + switch (type) { + case 'string': + actual = expected = 'Hello World'; + if (method === 'notStrictEqual') { + expected += 'bar'; + } + break; + case 'object': + actual = expected = { a: 'Hello', b: 'World' }; + if (method === 'notStrictEqual') { + expected = { a: 'Hello', b: 'World' }; + } + break; + case 'number': + actual = expected = 1e9; + if (method === 'notStrictEqual') { + expected += 1; + } + break; + default: + throw new Error('Unexpected type'); + } + + bench.start(); + for (let i = 0; i < n; ++i) { + fn(actual, expected); + } + bench.end(n); +} From c81456822cd1892cf4f0ccc2118cc63ec3f967ee Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Tue, 3 Sep 2024 11:44:23 -0300 Subject: [PATCH 2/4] benchmark: add throws and doesNotThrow bench --- benchmark/assert/throws.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 benchmark/assert/throws.js diff --git a/benchmark/assert/throws.js b/benchmark/assert/throws.js new file mode 100644 index 00000000000000..9c070ac8281551 --- /dev/null +++ b/benchmark/assert/throws.js @@ -0,0 +1,27 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + n: [25, 2e5], + method: ['throws', 'doesNotThrow'], +}); + +function main({ n, method }) { + const fn = assert[method]; + const shouldThrow = method === 'throws'; + + bench.start(); + for (let i = 0; i < n; ++i) { + fn(() => { + const err = new Error(`assert.${method}`); + if (shouldThrow) { + throw err; + } else { + return err; + } + }); + } + bench.end(n); +} From e8cbad5121bd4158a6ef85bf95965728c684740a Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Tue, 3 Sep 2024 11:47:22 -0300 Subject: [PATCH 3/4] benchmark: add rejects and doesNotReject bench --- benchmark/assert/rejects.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 benchmark/assert/rejects.js diff --git a/benchmark/assert/rejects.js b/benchmark/assert/rejects.js new file mode 100644 index 00000000000000..43ec500177a625 --- /dev/null +++ b/benchmark/assert/rejects.js @@ -0,0 +1,27 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + n: [25, 2e5], + method: ['rejects', 'doesNotReject'], +}); + +async function main({ n, method }) { + const fn = assert[method]; + const shouldReject = method === 'rejects'; + + bench.start(); + for (let i = 0; i < n; ++i) { + await fn(async () => { + const err = new Error(`assert.${method}`); + if (shouldReject) { + throw err; + } else { + return err; + } + }); + } + bench.end(n); +} From a2f1a3a51961935410ec5d07c240190d0a88cc3c Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Tue, 3 Sep 2024 11:50:34 -0300 Subject: [PATCH 4/4] benchmark: add match and doesNotMatch bench --- benchmark/assert/match.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 benchmark/assert/match.js diff --git a/benchmark/assert/match.js b/benchmark/assert/match.js new file mode 100644 index 00000000000000..fab86a23944c59 --- /dev/null +++ b/benchmark/assert/match.js @@ -0,0 +1,21 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + n: [25, 2e7], + method: ['match', 'doesNotMatch'], +}); + +function main({ n, method }) { + const fn = assert[method]; + const actual = 'Example of string that will match'; + const expected = method === 'match' ? /will match/ : /will not match/; + + bench.start(); + for (let i = 0; i < n; ++i) { + fn(actual, expected); + } + bench.end(n); +}