notes.dt.in.th

I played around with worker_threads in Node.js. This note documents some of my observations.

I am using the following code:

// index.mjs
import { Worker } from 'node:worker_threads'

for (;;) {
  const worker = new Worker('./f.mjs', {
    workerData: 10,
  })

  const result = await new Promise((resolve, reject) => {
    worker.on('message', resolve)
    worker.on('error', reject)
    worker.on('exit', (code) => {
      console.log('worker exit', code)
    })
  })

  console.log(result)

  await new Promise((r) => setTimeout(r, 1000))
}
// f.mjs
import { workerData, parentPort } from 'node:worker_threads'

parentPort.postMessage(workerData * workerData)

StackBlitz: https://stackblitz.com/edit/node-hr1nxh?file=index.mjs,f.mjs

Observations:

  • ESM just works.
  • They don’t share the same module cache.
  • Files are always loaded, so if I changed f.mjs, the next loop iteration would load the new version.