From 672e4ccf0507dd7893e36adb10929d99687dbcaa Mon Sep 17 00:00:00 2001 From: Khafra Date: Sat, 15 Jun 2024 23:40:30 -0400 Subject: [PATCH] lib: speed up MessageEvent creation internally PR-URL: https://github.com/nodejs/node/pull/52951 Refs: https://github.com/nodejs/undici/pull/3170 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Matteo Collina --- lib/internal/worker/io.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/internal/worker/io.js b/lib/internal/worker/io.js index 6a6e307a9e5008..3263ddb88194fc 100644 --- a/lib/internal/worker/io.js +++ b/lib/internal/worker/io.js @@ -85,9 +85,12 @@ const messageTypes = { LOAD_SCRIPT: 'loadScript', }; -let messageEvent; -function lazyMessageEvent() { - return messageEvent ??= require('internal/deps/undici/undici').MessageEvent; +// 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; + return fastCreateMessageEvent(type, init); } // We have to mess with the MessagePort prototype a bit, so that a) we can make @@ -128,7 +131,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 +324,7 @@ function receiveMessageOnPort(port) { } function onMessageEvent(type, data) { - this.dispatchEvent(new (lazyMessageEvent())(type, { data })); + this.dispatchEvent(lazyMessageEvent(type, { data })); } function isBroadcastChannel(value) {