From abf684c94c124242bade74d61be7a05308b50f22 Mon Sep 17 00:00:00 2001 From: Momtchil Momtchev Date: Wed, 14 Oct 2020 22:17:35 +0200 Subject: [PATCH] errors: add a benchmark for hidestackframes Add @puzpuzpuz's benchmark for the new implementation of hideStackFrames Refs: https://github.com/nodejs/node/issues/35386 Refs: https://github.com/nodejs/node/pull/35644 --- benchmark/misc/hidestackframes.js | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 benchmark/misc/hidestackframes.js diff --git a/benchmark/misc/hidestackframes.js b/benchmark/misc/hidestackframes.js new file mode 100644 index 00000000000000..58699f98d2ee5c --- /dev/null +++ b/benchmark/misc/hidestackframes.js @@ -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); +}