Skip to content

Commit

Permalink
errors: add a benchmark for hidestackframes
Browse files Browse the repository at this point in the history
Add @puzpuzpuz's benchmark for the new
implementation of hideStackFrames

Refs: nodejs#35386
Refs: nodejs#35644
  • Loading branch information
mmomtchev committed Dec 9, 2020
1 parent e0b4be9 commit 2dde837
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions benchmark/misc/hidestackframes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

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

/* there should be no significant performance differences
* between the hideStackFrames version and the direct
* call version
*/
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);
// eslint-disable-next-line no-unused-vars
} catch (e) {
// No-op
}
}

bench.end(n);
}

0 comments on commit 2dde837

Please sign in to comment.