Repository: nodejs/node
File: lib/internal/worker.js (JavaScript)

Code before:
      doEval,
      isInternal,
      cwdCounter: cwdCounter || workerIo.sharedCwdCounter,
      workerData: options.workerData,
      environmentData,
      publicPort: port2,
      hooksPort: !isInternal ? toHooksThread : undefined,
      manifestURL: getOptionValue('--experimental-policy') ?
        require('internal/process/policy').url :
        null,
      manifestSrc: getOptionValue('--experimental-policy') ?
        require('internal/process/policy').src :
        null,
      hasStdin: !!options.stdin,
    }, transferList);
    const loaderModule = require('internal/modules/esm/loader');
    const hasCustomizations = loaderModule.hasCustomizations();
    if (!isInternal && hasCustomizations) {
      // - send the second side of the channel to the hooks thread
      loaderModule.getHooksProxy().makeAsyncRequest(
        '#registerWorkerClient', [toWorkerThread], toWorkerThread);
    }
    // Use this to cache the Worker's loopStart value once available.
    this[kLoopStartTime] = -1;
    this[kIsOnline] = false;
    this.performance = {
      eventLoopUtilization: FunctionPrototypeBind(eventLoopUtilization, this),
    };
    // Actually start the new thread now that everything is in place.
    this[kHandle].startThread();

    process.nextTick(() => process.emit('worker', this));
    if (workerThreadsChannel.hasSubscribers) {
      workerThreadsChannel.publish({
        worker: this,
      });
    }
  }

  [kOnExit](code, customErr, customErrReason) {
    debug(`[${threadId}] hears end event for Worker ${this.threadId}`);
    drainMessagePort(this[kPublicPort]);
    drainMessagePort(this[kPort]);
    this.removeAllListeners('message');
    this.removeAllListeners('messageerrors');
    this[kPublicPort].unref();
    this[kPort].unref();
    this[kDispose]();
    if (customErr) {
      debug(`[${threadId}] failing with custom error ${customErr} \
        and with reason ${customErrReason}`);

Review comment (suggestion):
```suggestion
      loaderModule.getHooksProxy().makeAsyncRequest(
        '#registerWorkerClient',
        new Array(toWorkerThread),
        toWorkerThread,
      );
```

`new Array` here enables V8 to create a more optimised array under the hood, sized exactly right instead of oversized for potential additions.

Also, nit for formatting.

Code after:
      type: messageTypes.LOAD_SCRIPT,
      filename,
      doEval,
      isInternal,
      cwdCounter: cwdCounter || workerIo.sharedCwdCounter,
      workerData: options.workerData,
      environmentData,
      publicPort: port2,
      hooksPort: !isInternal ? toHooksThread : undefined,
      hasStdin: !!options.stdin,
    }, transferList);

    const loaderModule = require('internal/modules/esm/loader');
    const hasCustomizations = loaderModule.hasCustomizations();

    if (!isInternal && hasCustomizations) {
      // - send the second side of the channel to the hooks thread,
      // also announce the threadId of the Worker that will use that port.
      // This is needed for the cleanup stage
      loaderModule.getHooksProxy().makeSyncRequest(
        '#registerWorkerClient', [toWorkerThread], toWorkerThread, this.threadId);
    }
    // Use this to cache the Worker's loopStart value once available.
    this[kLoopStartTime] = -1;
    this[kIsOnline] = false;
    this.performance = {
      eventLoopUtilization: FunctionPrototypeBind(eventLoopUtilization, this),
    };
    // Actually start the new thread now that everything is in place.
    this[kHandle].startThread();

    process.nextTick(() => process.emit('worker', this));
    if (workerThreadsChannel.hasSubscribers) {
      workerThreadsChannel.publish({
        worker: this,
      });
    }
  }

  [kOnExit](code, customErr, customErrReason) {
    debug(`[${threadId}] hears end event for Worker ${this.threadId}`);
    const loaderModule = require('internal/modules/esm/loader');
    const hasCustomizations = loaderModule.hasCustomizations();

    if (!this.#isInternal && hasCustomizations) {
      loaderModule.getHooksProxy()?.makeAsyncRequest('#unregisterWorkerClient', undefined, this.threadId);
    }
    drainMessagePort(this[kPublicPort]);
    drainMessagePort(this[kPort]);
    this.removeAllListeners('message');
    this.removeAllListeners('messageerrors');