Skip to content

Commit

Permalink
perf: speed up MessageEvent creation internally
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed May 12, 2024
1 parent d78537b commit 98377de
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/internal/worker/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 98377de

Please sign in to comment.