From d90967b3466f2b8268eba750880d1265d799209a Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 21 Jul 2020 12:50:05 +0200 Subject: [PATCH] events: re-use the same isTrusted getter Creating a new function each time the property descriptor is set comes with performance overhead, since these functions have different identities, even if they contain the same code. Refs: https://twitter.com/tverwaes/status/1285496612618473472 PR-URL: https://github.com/nodejs/node/pull/34459 Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Yongsheng Zhang --- lib/internal/event_target.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index a55c9e461de458..a2a6452a3fcf60 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -7,6 +7,7 @@ const { Map, NumberIsInteger, Object, + ObjectDefineProperty, Symbol, SymbolFor, SymbolToStringTag, @@ -57,6 +58,9 @@ const kTimestamp = Symbol('timestamp'); const kBubbles = Symbol('bubbles'); const kComposed = Symbol('composed'); const kPropagationStopped = Symbol('propagationStopped'); + +const isTrusted = () => false; + class Event { constructor(type, options) { if (arguments.length === 0) @@ -72,8 +76,8 @@ class Event { this[kTimestamp] = lazyNow(); this[kPropagationStopped] = false; // isTrusted is special (LegacyUnforgeable) - Object.defineProperty(this, 'isTrusted', { - get() { return false; }, + ObjectDefineProperty(this, 'isTrusted', { + get: isTrusted, enumerable: true, configurable: false });