summaryrefslogtreecommitdiff
path: root/benchmark/misc/hidestackframes.js
blob: b28be725a30969ab18f28ab852f2933174579d77 (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
'use strict';

const common = require('../common.js');

const bench = common.createBenchmark(main, {
  type: ['hide-stackframes-throw', 'direct-call-throw',
         'hide-stackframes-noerr', 'direct-call-noerr'],
  n: [10e4],
}, {
  flags: ['--expose-internals'],
});

function main({ n, type }) {
  const {
    hideStackFrames,
    codes: {
      ERR_INVALID_ARG_TYPE,
    },
  } = require('internal/errors');

  const testfn = (value) => {
    if (typeof value !== 'number') {
      throw new ERR_INVALID_ARG_TYPE('Benchmark', 'number', value);
    }
  };

  let fn = testfn;
  if (type.startsWith('hide-stackframe'))
    fn = hideStackFrames(testfn);
  let value = 42;
  if (type.endsWith('-throw'))
    value = 'err';

  bench.start();

  for (let i = 0; i < n; i++) {
    try {
      fn(value);
    } catch {
      // No-op
    }
  }

  bench.end(n);
}