node/benchmark/assert/throws.js
Rich Trott f86e5fc437 benchmark: refactor benchmark/assert/throws.js
This is a minor refactor of benchmark/assert/throws.js to
reduce exceptions that need to be made for lint compliance.

PR-URL: https://github.com/nodejs/node/pull/21030
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
2018-06-02 05:17:29 +02:00

58 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common.js');
const { throws, doesNotThrow } = require('assert');
const bench = common.createBenchmark(main, {
n: [1e6],
method: [
'doesNotThrow',
'throws',
'throws_TypeError',
'throws_RegExp'
]
});
function main({ n, method }) {
const throwError = () => { throw new TypeError('foobar'); };
const doNotThrowError = () => { 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) {
doesNotThrow(doNotThrowError);
}
bench.end(n);
break;
case 'throws':
bench.start();
for (i = 0; i < n; ++i) {
throws(throwError);
}
bench.end(n);
break;
case 'throws_TypeError':
bench.start();
for (i = 0; i < n; ++i) {
throws(throwError, TypeError, message);
}
bench.end(n);
break;
case 'throws_RegExp':
bench.start();
for (i = 0; i < n; ++i) {
throws(throwError, regExp, message);
}
bench.end(n);
break;
default:
throw new Error(`Unsupported method ${method}`);
}
}