From 43aa76e11d697bc6114dd1141faa347b8fb27caf Mon Sep 17 00:00:00 2001 From: Khafra Date: Sat, 11 May 2024 22:49:27 -0400 Subject: [PATCH 1/2] lib: speed up MessageEvent creation internally --- lib/internal/worker/io.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/internal/worker/io.js b/lib/internal/worker/io.js index 6a6e307a9e5008..fb014d16b010c3 100644 --- a/lib/internal/worker/io.js +++ b/lib/internal/worker/io.js @@ -85,9 +85,10 @@ const messageTypes = { LOAD_SCRIPT: 'loadScript', }; -let messageEvent; -function lazyMessageEvent() { - return messageEvent ??= require('internal/deps/undici/undici').MessageEvent; +let fastCreateMessageEvent; +function lazyMessageEvent(type, init) { + fastCreateMessageEvent ??= require('internal/deps/undici/undici').createFastMessageEvent; + return fastCreateMessageEvent(type, init); } // We have to mess with the MessagePort prototype a bit, so that a) we can make @@ -128,7 +129,7 @@ ObjectDefineProperty( } const ports = this[kCurrentlyReceivingPorts]; this[kCurrentlyReceivingPorts] = undefined; - return new (lazyMessageEvent())(type, { data, ports }); + return lazyMessageEvent(type, { data, ports }); }, configurable: false, writable: false, @@ -321,7 +322,7 @@ function receiveMessageOnPort(port) { } function onMessageEvent(type, data) { - this.dispatchEvent(new (lazyMessageEvent())(type, { data })); + this.dispatchEvent(lazyMessageEvent(type, { data })); } function isBroadcastChannel(value) { From 29c44f31e5358f36f4bec0810942d28deab8afb7 Mon Sep 17 00:00:00 2001 From: Khafra Date: Mon, 13 May 2024 01:47:55 -0400 Subject: [PATCH 2/2] fixup --- lib/internal/worker/io.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/internal/worker/io.js b/lib/internal/worker/io.js index fb014d16b010c3..3263ddb88194fc 100644 --- a/lib/internal/worker/io.js +++ b/lib/internal/worker/io.js @@ -85,6 +85,8 @@ const messageTypes = { LOAD_SCRIPT: 'loadScript', }; +// createFastMessageEvent skips webidl argument validation when the arguments +// passed are known to be valid. let fastCreateMessageEvent; function lazyMessageEvent(type, init) { fastCreateMessageEvent ??= require('internal/deps/undici/undici').createFastMessageEvent;