node/benchmark/error/hidestackframes.js
Aras Abbasi cc725a653a
errors: improve performance of instantiation
PR-URL: https://github.com/nodejs/node/pull/49654
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
2023-09-28 09:57:38 +00:00

45 lines
859 B
JavaScript

'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);
}