• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common.js');
4const { throws, doesNotThrow } = require('assert');
5
6const bench = common.createBenchmark(main, {
7  n: [1e4],
8  method: [ 'doesNotThrow', 'throws_TypeError', 'throws_RegExp' ],
9});
10
11function main({ n, method }) {
12  const throwError = () => { throw new TypeError('foobar'); };
13  const doNotThrowError = () => { return 'foobar'; };
14  const regExp = /foobar/;
15  const message = 'failure';
16
17  switch (method) {
18    case 'doesNotThrow':
19      bench.start();
20      for (let i = 0; i < n; ++i) {
21        doesNotThrow(doNotThrowError);
22      }
23      bench.end(n);
24      break;
25    case 'throws_TypeError':
26      bench.start();
27      for (let i = 0; i < n; ++i) {
28        throws(throwError, TypeError, message);
29      }
30      bench.end(n);
31      break;
32    case 'throws_RegExp':
33      bench.start();
34      for (let i = 0; i < n; ++i) {
35        throws(throwError, regExp, message);
36      }
37      bench.end(n);
38      break;
39    default:
40      throw new Error(`Unsupported method ${method}`);
41  }
42}
43