From d40ea578178f3bc517a6abb88f6fe8dfd8d3d9df Mon Sep 17 00:00:00 2001 From: uzlopak Date: Thu, 14 Sep 2023 03:04:46 +0200 Subject: [PATCH 1/2] errors: improve classRegExp in errors.js --- benchmark/error/error-class-reg-exp.js | 23 +++++++++++++++++++++++ lib/internal/errors.js | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 benchmark/error/error-class-reg-exp.js diff --git a/benchmark/error/error-class-reg-exp.js b/benchmark/error/error-class-reg-exp.js new file mode 100644 index 00000000000000..14eaf727bc2350 --- /dev/null +++ b/benchmark/error/error-class-reg-exp.js @@ -0,0 +1,23 @@ +'use strict'; + +const common = require('../common.js'); + +const bench = common.createBenchmark(main, { + n: [1e5], +}, { + flags: ['--expose-internals'], +}); + +const instances = Array.from({ length: 1000 }).map(() => 'Uint8Array') + +function main({ n }) { + const { + codes: { + ERR_INVALID_ARG_TYPE, + }, + } = require('internal/errors'); + bench.start(); + for (let i = 0; i < n; ++i) + new ERR_INVALID_ARG_TYPE('target', instances, 'test') + bench.end(n); +} \ No newline at end of file diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 28c241f6fedda9..556df5e75dad0e 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -66,7 +66,8 @@ const isWindows = process.platform === 'win32'; const messages = new SafeMap(); const codes = {}; -const classRegExp = /^([A-Z][a-z0-9]*)+$/; +const classRegExp = /^[A-Z][a-zA-Z0-9]*$/; + // Sorted by a rough estimate on most frequently used entries. const kTypes = [ 'string', From 8a9e34c3f2769318108c93dc8497094c7cb6b722 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Thu, 14 Sep 2023 03:32:14 +0200 Subject: [PATCH 2/2] fix linting issue --- benchmark/error/error-class-reg-exp.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmark/error/error-class-reg-exp.js b/benchmark/error/error-class-reg-exp.js index 14eaf727bc2350..de8fced9d653f2 100644 --- a/benchmark/error/error-class-reg-exp.js +++ b/benchmark/error/error-class-reg-exp.js @@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, { flags: ['--expose-internals'], }); -const instances = Array.from({ length: 1000 }).map(() => 'Uint8Array') +const instances = Array.from({ length: 1000 }).map(() => 'Uint8Array'); function main({ n }) { const { @@ -18,6 +18,6 @@ function main({ n }) { } = require('internal/errors'); bench.start(); for (let i = 0; i < n; ++i) - new ERR_INVALID_ARG_TYPE('target', instances, 'test') + new ERR_INVALID_ARG_TYPE('target', instances, 'test'); bench.end(n); -} \ No newline at end of file +}