summaryrefslogtreecommitdiff
path: root/benchmark/assert/throws.js
blob: 075e227f886acc5ed4d372d71aeb29f472b8f354 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'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}`);
  }
}