benchmark: add throws and doesNotThrow bench
PR-URL: https://github.com/nodejs/node/pull/54734 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
This commit is contained in:
parent
688c885326
commit
0b283c2b97
27
benchmark/assert/throws.js
Normal file
27
benchmark/assert/throws.js
Normal file
@ -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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user