Skip to content

Commit

Permalink
bootstrap: load perf_hooks eagerly during bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed Jun 8, 2021
1 parent f0a70fa commit c59009e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
23 changes: 7 additions & 16 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,9 @@ if (!config.noBrowserGlobals) {

defineOperation(globalThis, 'queueMicrotask', queueMicrotask);

defineLazyGlobal(globalThis, 'performance', () => {
const { performance } = require('perf_hooks');
return performance;
});
// https://www.w3.org/TR/hr-time-2/#the-performance-attribute
defineReplacableAttribute(globalThis, 'performance',
require('perf_hooks').performance);

// Non-standard extensions:
defineOperation(globalThis, 'clearImmediate', timers.clearImmediate);
Expand Down Expand Up @@ -494,20 +493,12 @@ function defineOperation(target, name, method) {
});
}

function defineLazyGlobal(target, name, loader) {
let value;
let overridden = false;
// https://heycam.github.io/webidl/#Replaceable
function defineReplacableAttribute(target, name, value) {
ObjectDefineProperty(target, name, {
writable: true,
enumerable: true,
configurable: true,
get() {
if (value === undefined && !overridden)
value = loader();
return value;
},
set(val) {
value = val;
overridden = true;
}
value,
});
}
10 changes: 2 additions & 8 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,7 @@ const kRemoveListener = Symbol('kRemoveListener');
const kIsNodeStyleListener = Symbol('kIsNodeStyleListener');
const kTrustEvent = Symbol('kTrustEvent');

// Lazy load perf_hooks to avoid the additional overhead on startup
let perf_hooks;
function lazyNow() {
if (perf_hooks === undefined)
perf_hooks = require('perf_hooks');
return perf_hooks.performance.now();
}
const { now } = require('internal/perf/utils');

// TODO(joyeecheung): V8 snapshot does not support instance member
// initializers for now:
Expand Down Expand Up @@ -98,7 +92,7 @@ class Event {
this[kComposed] = !!composed;
this[kType] = `${type}`;
this[kDefaultPrevented] = false;
this[kTimestamp] = lazyNow();
this[kTimestamp] = now();
this[kPropagationStopped] = false;
if (options?.[kTrustEvent]) {
isTrustedSet.add(this);
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-bootstrap-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const expectedModules = new Set([
'NativeModule internal/modules/esm/translators',
'NativeModule internal/process/esm_loader',
'NativeModule internal/options',
'NativeModule internal/perf/event_loop_delay',
'NativeModule internal/perf/event_loop_utilization',
'NativeModule internal/perf/nodetiming',
'NativeModule internal/perf/observe',
Expand Down Expand Up @@ -126,6 +127,7 @@ const expectedModules = new Set([
'NativeModule internal/blob',
'NativeModule async_hooks',
'NativeModule path',
'NativeModule perf_hooks',
'NativeModule querystring',
'NativeModule stream',
'NativeModule stream/promises',
Expand Down

0 comments on commit c59009e

Please sign in to comment.