From 7169dc04ee938fd3b52e5a0e596dd4eb97a02acf Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 3 Jun 2020 18:13:01 -0700 Subject: [PATCH] events: lazy load perf_hooks for EventTarget MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/33717 Reviewed-By: Ruben Bridgewater Reviewed-By: Zeyu Yang Reviewed-By: Denys Otrishko Reviewed-By: Juan José Arboleda Reviewed-By: Benjamin Gruenbaum Reviewed-By: Trivikram Kamat --- lib/internal/event_target.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 8641129b132914..3df88cedf58a3d 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -19,7 +19,6 @@ const { } } = require('internal/errors'); -const perf_hooks = require('perf_hooks'); const { customInspectSymbol } = require('internal/util'); const { inspect } = require('util'); @@ -30,11 +29,19 @@ const kTarget = Symbol('kTarget'); const kNewListener = Symbol('kNewListener'); const kRemoveListener = Symbol('kRemoveListener'); +// 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(); +} + class Event { #type = undefined; #defaultPrevented = false; #cancelable = false; - #timestamp = perf_hooks.performance.now(); + #timestamp = lazyNow(); // None of these are currently used in the Node.js implementation // of EventTarget because there is no concept of bubbling or