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 <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Khafra
2024-06-15 23:40:30 -04:00
committed by GitHub
parent 75fe4f35d4
commit 672e4ccf05

View File

@@ -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) {