Skip to content

Commit de353b7

Browse files
joyeecheungtargos
authored andcommitted
perf_hooks: load internal/errors eagerly
Since `internal/errors` is loaded by many builtin modules and is currently the first module loaded during bootstrap, it is fine to load it eagerly. We just need to make sure that `internal/errors` itself load other modules lazily. PR-URL: #26771 Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 7022609 commit de353b7

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

lib/perf_hooks.js

+22-30
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ const L = require('internal/linkedlist');
3737
const kInspect = require('internal/util').customInspectSymbol;
3838
const { inherits } = require('util');
3939

40+
const {
41+
ERR_INVALID_CALLBACK,
42+
ERR_INVALID_ARG_VALUE,
43+
ERR_INVALID_ARG_TYPE,
44+
ERR_INVALID_OPT_VALUE,
45+
ERR_VALID_PERFORMANCE_ENTRY_TYPE,
46+
ERR_INVALID_PERFORMANCE_MARK
47+
} = require('internal/errors').codes;
48+
4049
const kHandle = Symbol('handle');
4150
const kMap = Symbol('map');
4251
const kCallback = Symbol('callback');
@@ -126,14 +135,6 @@ function collectHttp2Stats(entry) {
126135
}
127136
}
128137

129-
130-
let errors;
131-
function lazyErrors() {
132-
if (errors === undefined)
133-
errors = require('internal/errors').codes;
134-
return errors;
135-
}
136-
137138
function now() {
138139
const hr = process.hrtime();
139140
return hr[0] * 1000 + hr[1] / 1e6;
@@ -284,8 +285,7 @@ let gcTrackingIsEnabled = false;
284285
class PerformanceObserver extends AsyncResource {
285286
constructor(callback) {
286287
if (typeof callback !== 'function') {
287-
const errors = lazyErrors();
288-
throw new errors.ERR_INVALID_CALLBACK();
288+
throw new ERR_INVALID_CALLBACK();
289289
}
290290
super('PerformanceObserver');
291291
Object.defineProperties(this, {
@@ -331,16 +331,15 @@ class PerformanceObserver extends AsyncResource {
331331
}
332332

333333
observe(options) {
334-
const errors = lazyErrors();
335334
if (typeof options !== 'object' || options == null) {
336-
throw new errors.ERR_INVALID_ARG_TYPE('options', 'Object', options);
335+
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
337336
}
338337
if (!Array.isArray(options.entryTypes)) {
339-
throw new errors.ERR_INVALID_OPT_VALUE('entryTypes', options);
338+
throw new ERR_INVALID_OPT_VALUE('entryTypes', options);
340339
}
341340
const entryTypes = options.entryTypes.filter(filterTypes).map(mapTypes);
342341
if (entryTypes.length === 0) {
343-
throw new errors.ERR_VALID_PERFORMANCE_ENTRY_TYPE();
342+
throw new ERR_VALID_PERFORMANCE_ENTRY_TYPE();
344343
}
345344
if (entryTypes.includes(NODE_PERFORMANCE_ENTRY_TYPE_GC) &&
346345
!gcTrackingIsEnabled) {
@@ -393,8 +392,7 @@ class Performance {
393392
startMark = startMark !== undefined ? `${startMark}` : '';
394393
const marks = this[kIndex][kMarks];
395394
if (!marks.has(endMark) && !(endMark in nodeTiming)) {
396-
const errors = lazyErrors();
397-
throw new errors.ERR_INVALID_PERFORMANCE_MARK(endMark);
395+
throw new ERR_INVALID_PERFORMANCE_MARK(endMark);
398396
}
399397
_measure(name, startMark, endMark);
400398
}
@@ -412,8 +410,7 @@ class Performance {
412410

413411
timerify(fn) {
414412
if (typeof fn !== 'function') {
415-
const errors = lazyErrors();
416-
throw new errors.ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
413+
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
417414
}
418415
if (fn[kTimerified])
419416
return fn[kTimerified];
@@ -567,13 +564,11 @@ class ELDHistogram {
567564
get stddev() { return this[kHandle].stddev(); }
568565
percentile(percentile) {
569566
if (typeof percentile !== 'number') {
570-
const errors = lazyErrors();
571-
throw new errors.ERR_INVALID_ARG_TYPE('percentile', 'number', percentile);
567+
throw new ERR_INVALID_ARG_TYPE('percentile', 'number', percentile);
572568
}
573569
if (percentile <= 0 || percentile > 100) {
574-
const errors = lazyErrors();
575-
throw new errors.ERR_INVALID_ARG_VALUE.RangeError('percentile',
576-
percentile);
570+
throw new ERR_INVALID_ARG_VALUE.RangeError('percentile',
571+
percentile);
577572
}
578573
return this[kHandle].percentile(percentile);
579574
}
@@ -597,18 +592,15 @@ class ELDHistogram {
597592

598593
function monitorEventLoopDelay(options = {}) {
599594
if (typeof options !== 'object' || options === null) {
600-
const errors = lazyErrors();
601-
throw new errors.ERR_INVALID_ARG_TYPE('options', 'Object', options);
595+
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
602596
}
603597
const { resolution = 10 } = options;
604598
if (typeof resolution !== 'number') {
605-
const errors = lazyErrors();
606-
throw new errors.ERR_INVALID_ARG_TYPE('options.resolution',
607-
'number', resolution);
599+
throw new ERR_INVALID_ARG_TYPE('options.resolution',
600+
'number', resolution);
608601
}
609602
if (resolution <= 0 || !Number.isSafeInteger(resolution)) {
610-
const errors = lazyErrors();
611-
throw new errors.ERR_INVALID_OPT_VALUE.RangeError('resolution', resolution);
603+
throw new ERR_INVALID_OPT_VALUE.RangeError('resolution', resolution);
612604
}
613605
return new ELDHistogram(new _ELDHistogram(resolution));
614606
}

0 commit comments

Comments
 (0)