Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: speed up MessageEvent creation internally #52951

Merged
merged 2 commits into from
Jun 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
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