node/benchmark/assert/throws.js
Ruben Bridgewater 15bb8437fd
tools: add assert.doesNotThrow eslint rule
Prohibit the usage of `assert.doesNotThrow()`.

PR-URL: https://github.com/nodejs/node/pull/18669
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2018-02-16 16:54:57 +01:00

60 lines
1.4 KiB
JavaScript

'use strict';
const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
n: [1e6],
method: [
'doesNotThrow',
'throws',
'throws_TypeError',
'throws_RegExp'
]
});
function main({ n, method }) {
const throws = () => { throw new TypeError('foobar'); };
const doesNotThrow = () => { return 'foobar'; };
const regExp = /foobar/;
const message = 'failure';
var i;
switch (method) {
case '':
// Empty string falls through to next line as default, mostly for tests.
case 'doesNotThrow':
bench.start();
for (i = 0; i < n; ++i) {
// eslint-disable-next-line no-restricted-syntax
assert.doesNotThrow(doesNotThrow);
}
bench.end(n);
break;
case 'throws':
bench.start();
for (i = 0; i < n; ++i) {
// eslint-disable-next-line no-restricted-syntax
assert.throws(throws);
}
bench.end(n);
break;
case 'throws_TypeError':
bench.start();
for (i = 0; i < n; ++i) {
assert.throws(throws, TypeError, message);
}
bench.end(n);
break;
case 'throws_RegExp':
bench.start();
for (i = 0; i < n; ++i) {
assert.throws(throws, regExp, message);
}
bench.end(n);
break;
default:
throw new Error(`Unsupported method ${method}`);
}
}