Skip to content

Commit

Permalink
lib: speed up MessageEvent creation internally
Browse files Browse the repository at this point in the history
PR-URL: #52951
Refs: nodejs/undici#3170
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
KhafraDev authored and targos committed Jun 20, 2024
1 parent eded1e9 commit 452011b
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/internal/worker/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 452011b

Please sign in to comment.