0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/benchmark/assert/throws.js
Rich Trott 3c4c0db26a benchmark: provide default methods for assert
The benchmarks for `assert` all take a `method` configuration option,
but the allowable values are different across the files. For each
benchmark, provide an arbitrary default if `method` is set to an empty
string. This allows all the `assert` benchmarks to be run with a single
command but only on a single method. This is primarily useful for
testing that the assert benchmark files don't contain egregious errors.
(In other words, it's useful for testing.)

PR-URL: https://github.com/nodejs/node/pull/15174
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-09-15 15:34:39 -07:00

60 lines
1.3 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(conf) {
const n = +conf.n;
const throws = () => { throw new TypeError('foobar'); };
const doesNotThrow = () => { return 'foobar'; };
const regExp = /foobar/;
const message = 'failure';
var i;
switch (conf.method) {
case '':
// Empty string falls through to next line as default, mostly for tests.
case 'doesNotThrow':
bench.start();
for (i = 0; i < n; ++i) {
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 ${conf.method}`);
}
}