From 0b283c2b9776b977a15c417618d4bc44e6fc4f22 Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Tue, 3 Sep 2024 11:44:23 -0300 Subject: [PATCH] benchmark: add throws and doesNotThrow bench PR-URL: https://github.com/nodejs/node/pull/54734 Reviewed-By: Rich Trott Reviewed-By: Chemi Atlow --- 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 00000000000..9c070ac8281 --- /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); +}