Skip to content

Commit abf684c

Browse files
committed
errors: add a benchmark for hidestackframes
Add @puzpuzpuz's benchmark for the new implementation of hideStackFrames Refs: nodejs#35386 Refs: nodejs#35644
1 parent e47b84a commit abf684c

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

benchmark/misc/hidestackframes.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
5+
/* there should be no significant performance differences
6+
* between the hideStackFrames version and the direct
7+
* call version
8+
*/
9+
const bench = common.createBenchmark(main, {
10+
type: ['hide-stackframes-throw', 'direct-call-throw',
11+
'hide-stackframes-noerr', 'direct-call-noerr'],
12+
n: [10e4]
13+
}, {
14+
flags: ['--expose-internals']
15+
});
16+
17+
function main({ n, type }) {
18+
const {
19+
hideStackFrames,
20+
codes: {
21+
ERR_INVALID_ARG_TYPE,
22+
},
23+
} = require('internal/errors');
24+
25+
const testfn = (value) => {
26+
if (typeof value !== 'number') {
27+
throw new ERR_INVALID_ARG_TYPE('Benchmark', 'number', value);
28+
}
29+
};
30+
31+
let fn = testfn;
32+
if (type.startsWith('hide-stackframe'))
33+
fn = hideStackFrames(testfn);
34+
let value = 42;
35+
if (type.endsWith('-throw'))
36+
value = 'err';
37+
38+
bench.start();
39+
40+
for (let i = 0; i < n; i++) {
41+
try {
42+
fn(value);
43+
// eslint-disable-next-line no-unused-vars
44+
} catch (e) {
45+
// No-op
46+
}
47+
}
48+
49+
bench.end(n);
50+
}

0 commit comments

Comments
 (0)