Skip to content

Commit 672e4cc

Browse files
authored
lib: speed up MessageEvent creation internally
PR-URL: #52951 Refs: nodejs/undici#3170 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 75fe4f3 commit 672e4cc

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/internal/worker/io.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,12 @@ const messageTypes = {
8585
LOAD_SCRIPT: 'loadScript',
8686
};
8787

88-
let messageEvent;
89-
function lazyMessageEvent() {
90-
return messageEvent ??= require('internal/deps/undici/undici').MessageEvent;
88+
// createFastMessageEvent skips webidl argument validation when the arguments
89+
// passed are known to be valid.
90+
let fastCreateMessageEvent;
91+
function lazyMessageEvent(type, init) {
92+
fastCreateMessageEvent ??= require('internal/deps/undici/undici').createFastMessageEvent;
93+
return fastCreateMessageEvent(type, init);
9194
}
9295

9396
// We have to mess with the MessagePort prototype a bit, so that a) we can make
@@ -128,7 +131,7 @@ ObjectDefineProperty(
128131
}
129132
const ports = this[kCurrentlyReceivingPorts];
130133
this[kCurrentlyReceivingPorts] = undefined;
131-
return new (lazyMessageEvent())(type, { data, ports });
134+
return lazyMessageEvent(type, { data, ports });
132135
},
133136
configurable: false,
134137
writable: false,
@@ -321,7 +324,7 @@ function receiveMessageOnPort(port) {
321324
}
322325

323326
function onMessageEvent(type, data) {
324-
this.dispatchEvent(new (lazyMessageEvent())(type, { data }));
327+
this.dispatchEvent(lazyMessageEvent(type, { data }));
325328
}
326329

327330
function isBroadcastChannel(value) {

0 commit comments

Comments
 (0)