From 803f948f302ce93b89b93b875bef173f2a87c5fc Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 11 Dec 2023 16:23:53 +0100 Subject: [PATCH] perf: convert to arrow function tasks execution code path MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/worker/abstract-worker.ts | 10 +++++----- src/worker/cluster-worker.ts | 4 +++- src/worker/thread-worker.ts | 4 +++- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index ae95dcf8..dfcadf28 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -481,7 +481,7 @@ export abstract class AbstractWorker< * * @param task - The task to execute. */ - protected run (task: Task): void { + protected readonly run = (task: Task): void => { const { name, taskId, data } = task const taskFunctionName = name ?? DEFAULT_TASK_NAME if (!this.taskFunctions.has(taskFunctionName)) { @@ -509,10 +509,10 @@ export abstract class AbstractWorker< * @param fn - Task function that will be executed. * @param task - Input data for the task function. */ - protected runSync ( + protected readonly runSync = ( fn: TaskSyncFunction, task: Task - ): void { + ): void => { const { name, taskId, data } = task try { let taskPerformance = this.beginTaskPerformance(name) @@ -543,10 +543,10 @@ export abstract class AbstractWorker< * @param fn - Task function that will be executed. * @param task - Input data for the task function. */ - protected runAsync ( + protected readonly runAsync = ( fn: TaskAsyncFunction, task: Task - ): void { + ): void => { const { name, taskId, data } = task let taskPerformance = this.beginTaskPerformance(name) fn(data) diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index cafef7fc..d972692e 100644 --- a/src/worker/cluster-worker.ts +++ b/src/worker/cluster-worker.ts @@ -59,7 +59,9 @@ export class ClusterWorker< } /** @inheritDoc */ - protected sendToMainWorker (message: MessageValue): void { + protected readonly sendToMainWorker = ( + message: MessageValue + ): void => { this.getMainWorker().send({ ...message, workerId: this.id }) } } diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index 756550e3..e133430f 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -80,7 +80,9 @@ export class ThreadWorker< } /** @inheritDoc */ - protected sendToMainWorker (message: MessageValue): void { + protected readonly sendToMainWorker = ( + message: MessageValue + ): void => { this.port?.postMessage({ ...message, workerId: this.id }) } -- 2.34.1