From 676f56b8bbd18005c08cca163194010c5dccdc4d 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 --- 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 02ea2b8f0fff48..3fb83aaaac39dd 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, @@ -52,6 +53,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) @@ -67,8 +71,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 });