Skip to content

Commit

Permalink
errors: eliminate all overhead for hidden calls
Browse files Browse the repository at this point in the history
Eliminate all overhead for function calls that are to be
hidden from the stack traces at the expense of
reduced performance for the error case

Fixes: nodejs#35386

PR-URL: nodejs#35644
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Vladimir de Turckheim <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
mmomtchev authored and targos committed Aug 8, 2021
1 parent a87573c commit 9137798
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 148 deletions.
45 changes: 45 additions & 0 deletions benchmark/misc/hidestackframes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}
Loading

0 comments on commit 9137798

Please sign in to comment.