• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v18.20.1">
7  <title>Worker threads | Node.js v18.20.1 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/worker_threads.html">
12  <script async defer src="assets/api.js" type="text/javascript"></script>
13  <style>@media(max-width:326px){.with-13-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style>
14</head>
15<body class="alt apidoc" id="api-section-worker_threads">
16  <div id="content" class="clearfix">
17    <div id="column2" class="interior">
18      <div id="intro" class="interior">
19        <a href="/" title="Go back to the home page">
20          Node.js
21        </a>
22      </div>
23      <ul>
24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
26</ul>
27<hr class="line">
28<ul>
29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
33<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
38<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
39<li><a href="console.html" class="nav-console">Console</a></li>
40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
45<li><a href="dns.html" class="nav-dns">DNS</a></li>
46<li><a href="domain.html" class="nav-domain">Domain</a></li>
47<li><a href="errors.html" class="nav-errors">Errors</a></li>
48<li><a href="events.html" class="nav-events">Events</a></li>
49<li><a href="fs.html" class="nav-fs">File system</a></li>
50<li><a href="globals.html" class="nav-globals">Globals</a></li>
51<li><a href="http.html" class="nav-http">HTTP</a></li>
52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
53<li><a href="https.html" class="nav-https">HTTPS</a></li>
54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
55<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
60<li><a href="net.html" class="nav-net">Net</a></li>
61<li><a href="os.html" class="nav-os">OS</a></li>
62<li><a href="path.html" class="nav-path">Path</a></li>
63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
65<li><a href="process.html" class="nav-process">Process</a></li>
66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
68<li><a href="readline.html" class="nav-readline">Readline</a></li>
69<li><a href="repl.html" class="nav-repl">REPL</a></li>
70<li><a href="report.html" class="nav-report">Report</a></li>
71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
72<li><a href="stream.html" class="nav-stream">Stream</a></li>
73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
74<li><a href="test.html" class="nav-test">Test runner</a></li>
75<li><a href="timers.html" class="nav-timers">Timers</a></li>
76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
78<li><a href="tty.html" class="nav-tty">TTY</a></li>
79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
80<li><a href="url.html" class="nav-url">URL</a></li>
81<li><a href="util.html" class="nav-util">Utilities</a></li>
82<li><a href="v8.html" class="nav-v8">V8</a></li>
83<li><a href="vm.html" class="nav-vm">VM</a></li>
84<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
87<li><a href="worker_threads.html" class="nav-worker_threads active">Worker threads</a></li>
88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
89</ul>
90<hr class="line">
91<ul>
92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
93</ul>
94    </div>
95
96    <div id="column1" data-id="worker_threads" class="interior">
97      <header class="header">
98        <div class="header-container">
99          <h1>Node.js v18.20.1 documentation</h1>
100          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
101            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
102              <path fill="none" d="M0 0h24v24H0z" />
103              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
104              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
105            </svg>
106            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
107              <path d="M0 0h24v24H0z" fill="none" />
108              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
109            </svg>
110          </button>
111        </div>
112        <div id="gtoc">
113          <ul>
114            <li class="pinned-header">Node.js v18.20.1</li>
115
116    <li class="picker-header">
117      <a href="#">
118        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
119        Table of contents
120      </a>
121
122      <div class="picker"><div class="toc"><ul>
123<li><span class="stability_2"><a href="#worker-threads">Worker threads</a></span>
124<ul>
125<li><a href="#workergetenvironmentdatakey"><code>worker.getEnvironmentData(key)</code></a></li>
126<li><a href="#workerismainthread"><code>worker.isMainThread</code></a></li>
127<li><a href="#workermarkasuntransferableobject"><code>worker.markAsUntransferable(object)</code></a></li>
128<li><a href="#workermovemessageporttocontextport-contextifiedsandbox"><code>worker.moveMessagePortToContext(port, contextifiedSandbox)</code></a></li>
129<li><a href="#workerparentport"><code>worker.parentPort</code></a></li>
130<li><a href="#workerreceivemessageonportport"><code>worker.receiveMessageOnPort(port)</code></a></li>
131<li><a href="#workerresourcelimits"><code>worker.resourceLimits</code></a></li>
132<li><a href="#workershare_env"><code>worker.SHARE_ENV</code></a></li>
133<li><a href="#workersetenvironmentdatakey-value"><code>worker.setEnvironmentData(key[, value])</code></a></li>
134<li><a href="#workerthreadid"><code>worker.threadId</code></a></li>
135<li><a href="#workerworkerdata"><code>worker.workerData</code></a></li>
136<li><a href="#class-broadcastchannel-extends-eventtarget">Class: <code>BroadcastChannel extends EventTarget</code></a>
137<ul>
138<li><a href="#new-broadcastchannelname"><code>new BroadcastChannel(name)</code></a></li>
139<li><a href="#broadcastchannelclose"><code>broadcastChannel.close()</code></a></li>
140<li><a href="#broadcastchannelonmessage"><code>broadcastChannel.onmessage</code></a></li>
141<li><a href="#broadcastchannelonmessageerror"><code>broadcastChannel.onmessageerror</code></a></li>
142<li><a href="#broadcastchannelpostmessagemessage"><code>broadcastChannel.postMessage(message)</code></a></li>
143<li><a href="#broadcastchannelref"><code>broadcastChannel.ref()</code></a></li>
144<li><a href="#broadcastchannelunref"><code>broadcastChannel.unref()</code></a></li>
145</ul>
146</li>
147<li><a href="#class-messagechannel">Class: <code>MessageChannel</code></a></li>
148<li><a href="#class-messageport">Class: <code>MessagePort</code></a>
149<ul>
150<li><a href="#event-close">Event: <code>'close'</code></a></li>
151<li><a href="#event-message">Event: <code>'message'</code></a></li>
152<li><a href="#event-messageerror">Event: <code>'messageerror'</code></a></li>
153<li><a href="#portclose"><code>port.close()</code></a></li>
154<li><a href="#portpostmessagevalue-transferlist"><code>port.postMessage(value[, transferList])</code></a>
155<ul>
156<li><a href="#considerations-when-transferring-typedarrays-and-buffers">Considerations when transferring TypedArrays and Buffers</a></li>
157<li><a href="#considerations-when-cloning-objects-with-prototypes-classes-and-accessors">Considerations when cloning objects with prototypes, classes, and accessors</a></li>
158</ul>
159</li>
160<li><span class="stability_1"><a href="#porthasref"><code>port.hasRef()</code></a></span></li>
161<li><a href="#portref"><code>port.ref()</code></a></li>
162<li><a href="#portstart"><code>port.start()</code></a></li>
163<li><a href="#portunref"><code>port.unref()</code></a></li>
164</ul>
165</li>
166<li><a href="#class-worker">Class: <code>Worker</code></a>
167<ul>
168<li><a href="#new-workerfilename-options"><code>new Worker(filename[, options])</code></a></li>
169<li><a href="#event-error">Event: <code>'error'</code></a></li>
170<li><a href="#event-exit">Event: <code>'exit'</code></a></li>
171<li><a href="#event-message_1">Event: <code>'message'</code></a></li>
172<li><a href="#event-messageerror_1">Event: <code>'messageerror'</code></a></li>
173<li><a href="#event-online">Event: <code>'online'</code></a></li>
174<li><a href="#workergetheapsnapshot"><code>worker.getHeapSnapshot()</code></a></li>
175<li><a href="#workerperformance"><code>worker.performance</code></a>
176<ul>
177<li><a href="#performanceeventlooputilizationutilization1-utilization2"><code>performance.eventLoopUtilization([utilization1[, utilization2]])</code></a></li>
178</ul>
179</li>
180<li><a href="#workerpostmessagevalue-transferlist"><code>worker.postMessage(value[, transferList])</code></a></li>
181<li><a href="#workerref"><code>worker.ref()</code></a></li>
182<li><a href="#workerresourcelimits_1"><code>worker.resourceLimits</code></a></li>
183<li><a href="#workerstderr"><code>worker.stderr</code></a></li>
184<li><a href="#workerstdin"><code>worker.stdin</code></a></li>
185<li><a href="#workerstdout"><code>worker.stdout</code></a></li>
186<li><a href="#workerterminate"><code>worker.terminate()</code></a></li>
187<li><a href="#workerthreadid_1"><code>worker.threadId</code></a></li>
188<li><a href="#workerunref"><code>worker.unref()</code></a></li>
189</ul>
190</li>
191<li><a href="#notes">Notes</a>
192<ul>
193<li><a href="#synchronous-blocking-of-stdio">Synchronous blocking of stdio</a></li>
194<li><a href="#launching-worker-threads-from-preload-scripts">Launching worker threads from preload scripts</a></li>
195</ul>
196</li>
197</ul>
198</li>
199</ul></div></div>
200    </li>
201
202
203    <li class="picker-header">
204      <a href="#">
205        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
206        Index
207      </a>
208
209      <div class="picker"><ul>
210<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
211<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
212
213      <li>
214        <a href="index.html">Index</a>
215      </li>
216    </ul>
217
218<hr class="line">
219<ul>
220<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
221<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
222<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
223<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
224<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
225<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
226<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
227<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
228<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
229<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
230<li><a href="console.html" class="nav-console">Console</a></li>
231<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
232<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
233<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
234<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
235<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
236<li><a href="dns.html" class="nav-dns">DNS</a></li>
237<li><a href="domain.html" class="nav-domain">Domain</a></li>
238<li><a href="errors.html" class="nav-errors">Errors</a></li>
239<li><a href="events.html" class="nav-events">Events</a></li>
240<li><a href="fs.html" class="nav-fs">File system</a></li>
241<li><a href="globals.html" class="nav-globals">Globals</a></li>
242<li><a href="http.html" class="nav-http">HTTP</a></li>
243<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
244<li><a href="https.html" class="nav-https">HTTPS</a></li>
245<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
246<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
247<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
248<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
249<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
250<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
251<li><a href="net.html" class="nav-net">Net</a></li>
252<li><a href="os.html" class="nav-os">OS</a></li>
253<li><a href="path.html" class="nav-path">Path</a></li>
254<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
255<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
256<li><a href="process.html" class="nav-process">Process</a></li>
257<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
258<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
259<li><a href="readline.html" class="nav-readline">Readline</a></li>
260<li><a href="repl.html" class="nav-repl">REPL</a></li>
261<li><a href="report.html" class="nav-report">Report</a></li>
262<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
263<li><a href="stream.html" class="nav-stream">Stream</a></li>
264<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
265<li><a href="test.html" class="nav-test">Test runner</a></li>
266<li><a href="timers.html" class="nav-timers">Timers</a></li>
267<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
268<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
269<li><a href="tty.html" class="nav-tty">TTY</a></li>
270<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
271<li><a href="url.html" class="nav-url">URL</a></li>
272<li><a href="util.html" class="nav-util">Utilities</a></li>
273<li><a href="v8.html" class="nav-v8">V8</a></li>
274<li><a href="vm.html" class="nav-vm">VM</a></li>
275<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
276<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
277<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
278<li><a href="worker_threads.html" class="nav-worker_threads active">Worker threads</a></li>
279<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
280</ul>
281<hr class="line">
282<ul>
283<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
284</ul></div>
285    </li>
286
287
288    <li class="picker-header">
289      <a href="#">
290        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
291        Other versions
292      </a>
293      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/worker_threads.html">21.x</a></li>
294<li><a href="https://nodejs.org/docs/latest-v20.x/api/worker_threads.html">20.x <b>LTS</b></a></li>
295<li><a href="https://nodejs.org/docs/latest-v19.x/api/worker_threads.html">19.x</a></li>
296<li><a href="https://nodejs.org/docs/latest-v18.x/api/worker_threads.html">18.x <b>LTS</b></a></li>
297<li><a href="https://nodejs.org/docs/latest-v17.x/api/worker_threads.html">17.x</a></li>
298<li><a href="https://nodejs.org/docs/latest-v16.x/api/worker_threads.html">16.x</a></li>
299<li><a href="https://nodejs.org/docs/latest-v15.x/api/worker_threads.html">15.x</a></li>
300<li><a href="https://nodejs.org/docs/latest-v14.x/api/worker_threads.html">14.x</a></li>
301<li><a href="https://nodejs.org/docs/latest-v13.x/api/worker_threads.html">13.x</a></li>
302<li><a href="https://nodejs.org/docs/latest-v12.x/api/worker_threads.html">12.x</a></li>
303<li><a href="https://nodejs.org/docs/latest-v11.x/api/worker_threads.html">11.x</a></li>
304<li><a href="https://nodejs.org/docs/latest-v10.x/api/worker_threads.html">10.x</a></li></ol></div>
305    </li>
306
307            <li class="picker-header">
308              <a href="#">
309                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
310                Options
311              </a>
312
313              <div class="picker">
314                <ul>
315                  <li>
316                    <a href="all.html">View on single page</a>
317                  </li>
318                  <li>
319                    <a href="worker_threads.json">View as JSON</a>
320                  </li>
321                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/worker_threads.md">Edit on GitHub</a></li>
322                </ul>
323              </div>
324            </li>
325          </ul>
326        </div>
327        <hr>
328      </header>
329
330      <details id="toc" open><summary>Table of contents</summary><ul>
331<li><span class="stability_2"><a href="#worker-threads">Worker threads</a></span>
332<ul>
333<li><a href="#workergetenvironmentdatakey"><code>worker.getEnvironmentData(key)</code></a></li>
334<li><a href="#workerismainthread"><code>worker.isMainThread</code></a></li>
335<li><a href="#workermarkasuntransferableobject"><code>worker.markAsUntransferable(object)</code></a></li>
336<li><a href="#workermovemessageporttocontextport-contextifiedsandbox"><code>worker.moveMessagePortToContext(port, contextifiedSandbox)</code></a></li>
337<li><a href="#workerparentport"><code>worker.parentPort</code></a></li>
338<li><a href="#workerreceivemessageonportport"><code>worker.receiveMessageOnPort(port)</code></a></li>
339<li><a href="#workerresourcelimits"><code>worker.resourceLimits</code></a></li>
340<li><a href="#workershare_env"><code>worker.SHARE_ENV</code></a></li>
341<li><a href="#workersetenvironmentdatakey-value"><code>worker.setEnvironmentData(key[, value])</code></a></li>
342<li><a href="#workerthreadid"><code>worker.threadId</code></a></li>
343<li><a href="#workerworkerdata"><code>worker.workerData</code></a></li>
344<li><a href="#class-broadcastchannel-extends-eventtarget">Class: <code>BroadcastChannel extends EventTarget</code></a>
345<ul>
346<li><a href="#new-broadcastchannelname"><code>new BroadcastChannel(name)</code></a></li>
347<li><a href="#broadcastchannelclose"><code>broadcastChannel.close()</code></a></li>
348<li><a href="#broadcastchannelonmessage"><code>broadcastChannel.onmessage</code></a></li>
349<li><a href="#broadcastchannelonmessageerror"><code>broadcastChannel.onmessageerror</code></a></li>
350<li><a href="#broadcastchannelpostmessagemessage"><code>broadcastChannel.postMessage(message)</code></a></li>
351<li><a href="#broadcastchannelref"><code>broadcastChannel.ref()</code></a></li>
352<li><a href="#broadcastchannelunref"><code>broadcastChannel.unref()</code></a></li>
353</ul>
354</li>
355<li><a href="#class-messagechannel">Class: <code>MessageChannel</code></a></li>
356<li><a href="#class-messageport">Class: <code>MessagePort</code></a>
357<ul>
358<li><a href="#event-close">Event: <code>'close'</code></a></li>
359<li><a href="#event-message">Event: <code>'message'</code></a></li>
360<li><a href="#event-messageerror">Event: <code>'messageerror'</code></a></li>
361<li><a href="#portclose"><code>port.close()</code></a></li>
362<li><a href="#portpostmessagevalue-transferlist"><code>port.postMessage(value[, transferList])</code></a>
363<ul>
364<li><a href="#considerations-when-transferring-typedarrays-and-buffers">Considerations when transferring TypedArrays and Buffers</a></li>
365<li><a href="#considerations-when-cloning-objects-with-prototypes-classes-and-accessors">Considerations when cloning objects with prototypes, classes, and accessors</a></li>
366</ul>
367</li>
368<li><span class="stability_1"><a href="#porthasref"><code>port.hasRef()</code></a></span></li>
369<li><a href="#portref"><code>port.ref()</code></a></li>
370<li><a href="#portstart"><code>port.start()</code></a></li>
371<li><a href="#portunref"><code>port.unref()</code></a></li>
372</ul>
373</li>
374<li><a href="#class-worker">Class: <code>Worker</code></a>
375<ul>
376<li><a href="#new-workerfilename-options"><code>new Worker(filename[, options])</code></a></li>
377<li><a href="#event-error">Event: <code>'error'</code></a></li>
378<li><a href="#event-exit">Event: <code>'exit'</code></a></li>
379<li><a href="#event-message_1">Event: <code>'message'</code></a></li>
380<li><a href="#event-messageerror_1">Event: <code>'messageerror'</code></a></li>
381<li><a href="#event-online">Event: <code>'online'</code></a></li>
382<li><a href="#workergetheapsnapshot"><code>worker.getHeapSnapshot()</code></a></li>
383<li><a href="#workerperformance"><code>worker.performance</code></a>
384<ul>
385<li><a href="#performanceeventlooputilizationutilization1-utilization2"><code>performance.eventLoopUtilization([utilization1[, utilization2]])</code></a></li>
386</ul>
387</li>
388<li><a href="#workerpostmessagevalue-transferlist"><code>worker.postMessage(value[, transferList])</code></a></li>
389<li><a href="#workerref"><code>worker.ref()</code></a></li>
390<li><a href="#workerresourcelimits_1"><code>worker.resourceLimits</code></a></li>
391<li><a href="#workerstderr"><code>worker.stderr</code></a></li>
392<li><a href="#workerstdin"><code>worker.stdin</code></a></li>
393<li><a href="#workerstdout"><code>worker.stdout</code></a></li>
394<li><a href="#workerterminate"><code>worker.terminate()</code></a></li>
395<li><a href="#workerthreadid_1"><code>worker.threadId</code></a></li>
396<li><a href="#workerunref"><code>worker.unref()</code></a></li>
397</ul>
398</li>
399<li><a href="#notes">Notes</a>
400<ul>
401<li><a href="#synchronous-blocking-of-stdio">Synchronous blocking of stdio</a></li>
402<li><a href="#launching-worker-threads-from-preload-scripts">Launching worker threads from preload scripts</a></li>
403</ul>
404</li>
405</ul>
406</li>
407</ul></details>
408
409      <div id="apicontent">
410        <h2>Worker threads<span><a class="mark" href="#worker-threads" id="worker-threads">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_threads"></a></h2>
411
412<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
413<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/worker_threads.js">lib/worker_threads.js</a></p>
414<p>The <code>node:worker_threads</code> module enables the use of threads that execute
415JavaScript in parallel. To access it:</p>
416<pre><code class="language-js"><span class="hljs-keyword">const</span> worker = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);</code> <button class="copy-button">copy</button></pre>
417<p>Workers (threads) are useful for performing CPU-intensive JavaScript operations.
418They do not help much with I/O-intensive work. The Node.js built-in
419asynchronous I/O operations are more efficient than Workers can be.</p>
420<p>Unlike <code>child_process</code> or <code>cluster</code>, <code>worker_threads</code> can share memory. They do
421so by transferring <code>ArrayBuffer</code> instances or sharing <code>SharedArrayBuffer</code>
422instances.</p>
423<pre><code class="language-js"><span class="hljs-keyword">const</span> {
424  <span class="hljs-title class_">Worker</span>, isMainThread, parentPort, workerData,
425} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
426
427<span class="hljs-keyword">if</span> (isMainThread) {
428  <span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-keyword">function</span> <span class="hljs-title function_">parseJSAsync</span>(<span class="hljs-params">script</span>) {
429    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =></span> {
430      <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename, {
431        <span class="hljs-attr">workerData</span>: script,
432      });
433      worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, resolve);
434      worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, reject);
435      worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> {
436        <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>)
437          <span class="hljs-title function_">reject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">`Worker stopped with exit code <span class="hljs-subst">${code}</span>`</span>));
438      });
439    });
440  };
441} <span class="hljs-keyword">else</span> {
442  <span class="hljs-keyword">const</span> { parse } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'some-js-parsing-library'</span>);
443  <span class="hljs-keyword">const</span> script = workerData;
444  parentPort.<span class="hljs-title function_">postMessage</span>(<span class="hljs-title function_">parse</span>(script));
445}</code> <button class="copy-button">copy</button></pre>
446<p>The above example spawns a Worker thread for each <code>parseJSAsync()</code> call. In
447practice, use a pool of Workers for these kinds of tasks. Otherwise, the
448overhead of creating Workers would likely exceed their benefit.</p>
449<p>When implementing a worker pool, use the <a href="async_hooks.html#class-asyncresource"><code>AsyncResource</code></a> API to inform
450diagnostic tools (e.g. to provide asynchronous stack traces) about the
451correlation between tasks and their outcomes. See
452<a href="async_context.html#using-asyncresource-for-a-worker-thread-pool">"Using <code>AsyncResource</code> for a <code>Worker</code> thread pool"</a>
453in the <code>async_hooks</code> documentation for an example implementation.</p>
454<p>Worker threads inherit non-process-specific options by default. Refer to
455<a href="#new-workerfilename-options"><code>Worker constructor options</code></a> to know how to customize worker thread options,
456specifically <code>argv</code> and <code>execArgv</code> options.</p>
457<section><h3><code>worker.getEnvironmentData(key)</code><span><a class="mark" href="#workergetenvironmentdatakey" id="workergetenvironmentdatakey">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_getenvironmentdata_key"></a></h3>
458<div class="api_metadata">
459<details class="changelog"><summary>History</summary>
460<table>
461<tbody><tr><th>Version</th><th>Changes</th></tr>
462<tr><td>v17.5.0</td>
463<td><p>No longer experimental.</p></td></tr>
464<tr><td>v15.12.0, v14.18.0</td>
465<td><p><span>Added in: v15.12.0, v14.18.0</span></p></td></tr>
466</tbody></table>
467</details>
468</div>
469<ul>
470<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> Any arbitrary, cloneable JavaScript value that can be used as a
471<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" class="type">&#x3C;Map></a> key.</li>
472<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
473</ul>
474<p>Within a worker thread, <code>worker.getEnvironmentData()</code> returns a clone
475of data passed to the spawning thread's <code>worker.setEnvironmentData()</code>.
476Every new <code>Worker</code> receives its own copy of the environment data
477automatically.</p>
478<pre><code class="language-js"><span class="hljs-keyword">const</span> {
479  <span class="hljs-title class_">Worker</span>,
480  isMainThread,
481  setEnvironmentData,
482  getEnvironmentData,
483} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
484
485<span class="hljs-keyword">if</span> (isMainThread) {
486  <span class="hljs-title function_">setEnvironmentData</span>(<span class="hljs-string">'Hello'</span>, <span class="hljs-string">'World!'</span>);
487  <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename);
488} <span class="hljs-keyword">else</span> {
489  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getEnvironmentData</span>(<span class="hljs-string">'Hello'</span>));  <span class="hljs-comment">// Prints 'World!'.</span>
490}</code> <button class="copy-button">copy</button></pre>
491</section><section><h3><code>worker.isMainThread</code><span><a class="mark" href="#workerismainthread" id="workerismainthread">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_ismainthread"></a></h3>
492<div class="api_metadata">
493<span>Added in: v10.5.0</span>
494</div>
495<ul>
496<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
497</ul>
498<p>Is <code>true</code> if this code is not running inside of a <a href="#class-worker"><code>Worker</code></a> thread.</p>
499<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMainThread } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
500
501<span class="hljs-keyword">if</span> (isMainThread) {
502  <span class="hljs-comment">// This re-loads the current file inside a Worker instance.</span>
503  <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename);
504} <span class="hljs-keyword">else</span> {
505  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Inside Worker!'</span>);
506  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(isMainThread);  <span class="hljs-comment">// Prints 'false'.</span>
507}</code> <button class="copy-button">copy</button></pre>
508</section><section><h3><code>worker.markAsUntransferable(object)</code><span><a class="mark" href="#workermarkasuntransferableobject" id="workermarkasuntransferableobject">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_markasuntransferable_object"></a></h3>
509<div class="api_metadata">
510<span>Added in: v14.5.0, v12.19.0</span>
511</div>
512<p>Mark an object as not transferable. If <code>object</code> occurs in the transfer list of
513a <a href="#portpostmessagevalue-transferlist"><code>port.postMessage()</code></a> call, it is ignored.</p>
514<p>In particular, this makes sense for objects that can be cloned, rather than
515transferred, and which are used by other objects on the sending side.
516For example, Node.js marks the <code>ArrayBuffer</code>s it uses for its
517<a href="buffer.html#static-method-bufferallocunsafesize"><code>Buffer</code> pool</a> with this.</p>
518<p>This operation cannot be undone.</p>
519<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</span>, markAsUntransferable } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
520
521<span class="hljs-keyword">const</span> pooledBuffer = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">8</span>);
522<span class="hljs-keyword">const</span> typedArray1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(pooledBuffer);
523<span class="hljs-keyword">const</span> typedArray2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Float64Array</span>(pooledBuffer);
524
525<span class="hljs-title function_">markAsUntransferable</span>(pooledBuffer);
526
527<span class="hljs-keyword">const</span> { port1 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
528port1.<span class="hljs-title function_">postMessage</span>(typedArray1, [ typedArray1.<span class="hljs-property">buffer</span> ]);
529
530<span class="hljs-comment">// The following line prints the contents of typedArray1 -- it still owns</span>
531<span class="hljs-comment">// its memory and has been cloned, not transferred. Without</span>
532<span class="hljs-comment">// `markAsUntransferable()`, this would print an empty Uint8Array.</span>
533<span class="hljs-comment">// typedArray2 is intact as well.</span>
534<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(typedArray1);
535<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(typedArray2);</code> <button class="copy-button">copy</button></pre>
536<p>There is no equivalent to this API in browsers.</p>
537</section><section><h3><code>worker.moveMessagePortToContext(port, contextifiedSandbox)</code><span><a class="mark" href="#workermovemessageporttocontextport-contextifiedsandbox" id="workermovemessageporttocontextport-contextifiedsandbox">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_movemessageporttocontext_port_contextifiedsandbox"></a></h3>
538<div class="api_metadata">
539<span>Added in: v11.13.0</span>
540</div>
541<ul>
542<li>
543<p><code>port</code> <a href="worker_threads.html#class-messageport" class="type">&#x3C;MessagePort></a> The message port to transfer.</p>
544</li>
545<li>
546<p><code>contextifiedSandbox</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> A <a href="vm.html#what-does-it-mean-to-contextify-an-object">contextified</a> object as returned by the
547<code>vm.createContext()</code> method.</p>
548</li>
549<li>
550<p>Returns: <a href="worker_threads.html#class-messageport" class="type">&#x3C;MessagePort></a></p>
551</li>
552</ul>
553<p>Transfer a <code>MessagePort</code> to a different <a href="vm.html"><code>vm</code></a> Context. The original <code>port</code>
554object is rendered unusable, and the returned <code>MessagePort</code> instance
555takes its place.</p>
556<p>The returned <code>MessagePort</code> is an object in the target context and
557inherits from its global <code>Object</code> class. Objects passed to the
558<a href="https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/onmessage"><code>port.onmessage()</code></a> listener are also created in the target context
559and inherit from its global <code>Object</code> class.</p>
560<p>However, the created <code>MessagePort</code> no longer inherits from
561<a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget"><code>EventTarget</code></a>, and only <a href="https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/onmessage"><code>port.onmessage()</code></a> can be used to receive
562events using it.</p>
563</section><section><h3><code>worker.parentPort</code><span><a class="mark" href="#workerparentport" id="workerparentport">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_parentport"></a></h3>
564<div class="api_metadata">
565<span>Added in: v10.5.0</span>
566</div>
567<ul>
568<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> | <a href="worker_threads.html#class-messageport" class="type">&#x3C;MessagePort></a></li>
569</ul>
570<p>If this thread is a <a href="#class-worker"><code>Worker</code></a>, this is a <a href="#class-messageport"><code>MessagePort</code></a>
571allowing communication with the parent thread. Messages sent using
572<code>parentPort.postMessage()</code> are available in the parent thread
573using <code>worker.on('message')</code>, and messages sent from the parent thread
574using <code>worker.postMessage()</code> are available in this thread using
575<code>parentPort.on('message')</code>.</p>
576<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMainThread, parentPort } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
577
578<span class="hljs-keyword">if</span> (isMainThread) {
579  <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename);
580  worker.<span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">message</span>) =></span> {
581    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(message);  <span class="hljs-comment">// Prints 'Hello, world!'.</span>
582  });
583  worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'Hello, world!'</span>);
584} <span class="hljs-keyword">else</span> {
585  <span class="hljs-comment">// When a message from the parent thread is received, send it back:</span>
586  parentPort.<span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">message</span>) =></span> {
587    parentPort.<span class="hljs-title function_">postMessage</span>(message);
588  });
589}</code> <button class="copy-button">copy</button></pre>
590</section><section><h3><code>worker.receiveMessageOnPort(port)</code><span><a class="mark" href="#workerreceivemessageonportport" id="workerreceivemessageonportport">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_receivemessageonport_port"></a></h3>
591<div class="api_metadata">
592<details class="changelog"><summary>History</summary>
593<table>
594<tbody><tr><th>Version</th><th>Changes</th></tr>
595<tr><td>v15.12.0</td>
596<td><p>The port argument can also refer to a <code>BroadcastChannel</code> now.</p></td></tr>
597<tr><td>v12.3.0</td>
598<td><p><span>Added in: v12.3.0</span></p></td></tr>
599</tbody></table>
600</details>
601</div>
602<ul>
603<li>
604<p><code>port</code> <a href="worker_threads.html#class-messageport" class="type">&#x3C;MessagePort></a> | <a href="worker_threads.html#class-broadcastchannel-extends-eventtarget" class="type">&#x3C;BroadcastChannel></a></p>
605</li>
606<li>
607<p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></p>
608</li>
609</ul>
610<p>Receive a single message from a given <code>MessagePort</code>. If no message is available,
611<code>undefined</code> is returned, otherwise an object with a single <code>message</code> property
612that contains the message payload, corresponding to the oldest message in the
613<code>MessagePort</code>'s queue.</p>
614<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</span>, receiveMessageOnPort } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
615<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
616port1.<span class="hljs-title function_">postMessage</span>({ <span class="hljs-attr">hello</span>: <span class="hljs-string">'world'</span> });
617
618<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">receiveMessageOnPort</span>(port2));
619<span class="hljs-comment">// Prints: { message: { hello: 'world' } }</span>
620<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">receiveMessageOnPort</span>(port2));
621<span class="hljs-comment">// Prints: undefined</span></code> <button class="copy-button">copy</button></pre>
622<p>When this function is used, no <code>'message'</code> event is emitted and the
623<code>onmessage</code> listener is not invoked.</p>
624</section><section><h3><code>worker.resourceLimits</code><span><a class="mark" href="#workerresourcelimits" id="workerresourcelimits">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_resourcelimits"></a></h3>
625<div class="api_metadata">
626<span>Added in: v13.2.0, v12.16.0</span>
627</div>
628<ul>
629<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
630<ul>
631<li><code>maxYoungGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
632<li><code>maxOldGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
633<li><code>codeRangeSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
634<li><code>stackSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
635</ul>
636</li>
637</ul>
638<p>Provides the set of JS engine resource constraints inside this Worker thread.
639If the <code>resourceLimits</code> option was passed to the <a href="#class-worker"><code>Worker</code></a> constructor,
640this matches its values.</p>
641<p>If this is used in the main thread, its value is an empty object.</p>
642</section><section><h3><code>worker.SHARE_ENV</code><span><a class="mark" href="#workershare_env" id="workershare_env">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_share_env"></a></h3>
643<div class="api_metadata">
644<span>Added in: v11.14.0</span>
645</div>
646<ul>
647<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a></li>
648</ul>
649<p>A special value that can be passed as the <code>env</code> option of the <a href="#class-worker"><code>Worker</code></a>
650constructor, to indicate that the current thread and the Worker thread should
651share read and write access to the same set of environment variables.</p>
652<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, <span class="hljs-variable constant_">SHARE_ENV</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
653<span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(<span class="hljs-string">'process.env.SET_IN_WORKER = "foo"'</span>, { <span class="hljs-attr">eval</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">env</span>: <span class="hljs-variable constant_">SHARE_ENV</span> })
654  .<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> {
655    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-property">env</span>.<span class="hljs-property">SET_IN_WORKER</span>);  <span class="hljs-comment">// Prints 'foo'.</span>
656  });</code> <button class="copy-button">copy</button></pre>
657</section><section><h3><code>worker.setEnvironmentData(key[, value])</code><span><a class="mark" href="#workersetenvironmentdatakey-value" id="workersetenvironmentdatakey-value">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_setenvironmentdata_key_value"></a></h3>
658<div class="api_metadata">
659<details class="changelog"><summary>History</summary>
660<table>
661<tbody><tr><th>Version</th><th>Changes</th></tr>
662<tr><td>v17.5.0</td>
663<td><p>No longer experimental.</p></td></tr>
664<tr><td>v15.12.0, v14.18.0</td>
665<td><p><span>Added in: v15.12.0, v14.18.0</span></p></td></tr>
666</tbody></table>
667</details>
668</div>
669<ul>
670<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> Any arbitrary, cloneable JavaScript value that can be used as a
671<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" class="type">&#x3C;Map></a> key.</li>
672<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> Any arbitrary, cloneable JavaScript value that will be cloned
673and passed automatically to all new <code>Worker</code> instances. If <code>value</code> is passed
674as <code>undefined</code>, any previously set value for the <code>key</code> will be deleted.</li>
675</ul>
676<p>The <code>worker.setEnvironmentData()</code> API sets the content of
677<code>worker.getEnvironmentData()</code> in the current thread and all new <code>Worker</code>
678instances spawned from the current context.</p>
679</section><section><h3><code>worker.threadId</code><span><a class="mark" href="#workerthreadid" id="workerthreadid">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_threadid"></a></h3>
680<div class="api_metadata">
681<span>Added in: v10.5.0</span>
682</div>
683<ul>
684<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
685</ul>
686<p>An integer identifier for the current thread. On the corresponding worker object
687(if there is any), it is available as <a href="#workerthreadid_1"><code>worker.threadId</code></a>.
688This value is unique for each <a href="#class-worker"><code>Worker</code></a> instance inside a single process.</p>
689</section><section><h3><code>worker.workerData</code><span><a class="mark" href="#workerworkerdata" id="workerworkerdata">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_workerdata"></a></h3>
690<div class="api_metadata">
691<span>Added in: v10.5.0</span>
692</div>
693<p>An arbitrary JavaScript value that contains a clone of the data passed
694to this thread's <code>Worker</code> constructor.</p>
695<p>The data is cloned as if using <a href="#portpostmessagevalue-transferlist"><code>postMessage()</code></a>,
696according to the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm">HTML structured clone algorithm</a>.</p>
697<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMainThread, workerData } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
698
699<span class="hljs-keyword">if</span> (isMainThread) {
700  <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename, { <span class="hljs-attr">workerData</span>: <span class="hljs-string">'Hello, world!'</span> });
701} <span class="hljs-keyword">else</span> {
702  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(workerData);  <span class="hljs-comment">// Prints 'Hello, world!'.</span>
703}</code> <button class="copy-button">copy</button></pre>
704</section><section><h3>Class: <code>BroadcastChannel extends EventTarget</code><span><a class="mark" href="#class-broadcastchannel-extends-eventtarget" id="class-broadcastchannel-extends-eventtarget">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_class_broadcastchannel_extends_eventtarget"></a></h3>
705<div class="api_metadata">
706<details class="changelog"><summary>History</summary>
707<table>
708<tbody><tr><th>Version</th><th>Changes</th></tr>
709<tr><td>v18.0.0</td>
710<td><p>No longer experimental.</p></td></tr>
711<tr><td>v15.4.0</td>
712<td><p><span>Added in: v15.4.0</span></p></td></tr>
713</tbody></table>
714</details>
715</div>
716<p>Instances of <code>BroadcastChannel</code> allow asynchronous one-to-many communication
717with all other <code>BroadcastChannel</code> instances bound to the same channel name.</p>
718<pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
719
720<span class="hljs-keyword">const</span> {
721  isMainThread,
722  <span class="hljs-title class_">BroadcastChannel</span>,
723  <span class="hljs-title class_">Worker</span>,
724} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
725
726<span class="hljs-keyword">const</span> bc = <span class="hljs-keyword">new</span> <span class="hljs-title class_">BroadcastChannel</span>(<span class="hljs-string">'hello'</span>);
727
728<span class="hljs-keyword">if</span> (isMainThread) {
729  <span class="hljs-keyword">let</span> c = <span class="hljs-number">0</span>;
730  bc.<span class="hljs-property">onmessage</span> = <span class="hljs-function">(<span class="hljs-params">event</span>) =></span> {
731    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span class="hljs-property">data</span>);
732    <span class="hljs-keyword">if</span> (++c === <span class="hljs-number">10</span>) bc.<span class="hljs-title function_">close</span>();
733  };
734  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-number">0</span>; n &#x3C; <span class="hljs-number">10</span>; n++)
735    <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename);
736} <span class="hljs-keyword">else</span> {
737  bc.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'hello from every worker'</span>);
738  bc.<span class="hljs-title function_">close</span>();
739}</code> <button class="copy-button">copy</button></pre>
740<h4><code>new BroadcastChannel(name)</code><span><a class="mark" href="#new-broadcastchannelname" id="new-broadcastchannelname">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_new_broadcastchannel_name"></a></h4>
741<div class="api_metadata">
742<span>Added in: v15.4.0</span>
743</div>
744<ul>
745<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> The name of the channel to connect to. Any JavaScript value
746that can be converted to a string using <code>`${name}`</code> is permitted.</li>
747</ul>
748<h4><code>broadcastChannel.close()</code><span><a class="mark" href="#broadcastchannelclose" id="broadcastchannelclose">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_broadcastchannel_close"></a></h4>
749<div class="api_metadata">
750<span>Added in: v15.4.0</span>
751</div>
752<p>Closes the <code>BroadcastChannel</code> connection.</p>
753<h4><code>broadcastChannel.onmessage</code><span><a class="mark" href="#broadcastchannelonmessage" id="broadcastchannelonmessage">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_broadcastchannel_onmessage"></a></h4>
754<div class="api_metadata">
755<span>Added in: v15.4.0</span>
756</div>
757<ul>
758<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Invoked with a single <code>MessageEvent</code> argument
759when a message is received.</li>
760</ul>
761<h4><code>broadcastChannel.onmessageerror</code><span><a class="mark" href="#broadcastchannelonmessageerror" id="broadcastchannelonmessageerror">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_broadcastchannel_onmessageerror"></a></h4>
762<div class="api_metadata">
763<span>Added in: v15.4.0</span>
764</div>
765<ul>
766<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Invoked with a received message cannot be
767deserialized.</li>
768</ul>
769<h4><code>broadcastChannel.postMessage(message)</code><span><a class="mark" href="#broadcastchannelpostmessagemessage" id="broadcastchannelpostmessagemessage">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_broadcastchannel_postmessage_message"></a></h4>
770<div class="api_metadata">
771<span>Added in: v15.4.0</span>
772</div>
773<ul>
774<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> Any cloneable JavaScript value.</li>
775</ul>
776<h4><code>broadcastChannel.ref()</code><span><a class="mark" href="#broadcastchannelref" id="broadcastchannelref">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_broadcastchannel_ref"></a></h4>
777<div class="api_metadata">
778<span>Added in: v15.4.0</span>
779</div>
780<p>Opposite of <code>unref()</code>. Calling <code>ref()</code> on a previously <code>unref()</code>ed
781BroadcastChannel does <em>not</em> let the program exit if it's the only active handle
782left (the default behavior). If the port is <code>ref()</code>ed, calling <code>ref()</code> again
783has no effect.</p>
784<h4><code>broadcastChannel.unref()</code><span><a class="mark" href="#broadcastchannelunref" id="broadcastchannelunref">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_broadcastchannel_unref"></a></h4>
785<div class="api_metadata">
786<span>Added in: v15.4.0</span>
787</div>
788<p>Calling <code>unref()</code> on a BroadcastChannel allows the thread to exit if this
789is the only active handle in the event system. If the BroadcastChannel is
790already <code>unref()</code>ed calling <code>unref()</code> again has no effect.</p>
791</section><section><h3>Class: <code>MessageChannel</code><span><a class="mark" href="#class-messagechannel" id="class-messagechannel">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_class_messagechannel"></a></h3>
792<div class="api_metadata">
793<span>Added in: v10.5.0</span>
794</div>
795<p>Instances of the <code>worker.MessageChannel</code> class represent an asynchronous,
796two-way communications channel.
797The <code>MessageChannel</code> has no methods of its own. <code>new MessageChannel()</code>
798yields an object with <code>port1</code> and <code>port2</code> properties, which refer to linked
799<a href="#class-messageport"><code>MessagePort</code></a> instances.</p>
800<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
801
802<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
803port1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">message</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'received'</span>, message));
804port2.<span class="hljs-title function_">postMessage</span>({ <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span> });
805<span class="hljs-comment">// Prints: received { foo: 'bar' } from the `port1.on('message')` listener</span></code> <button class="copy-button">copy</button></pre>
806</section><section><h3>Class: <code>MessagePort</code><span><a class="mark" href="#class-messageport" id="class-messageport">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_class_messageport"></a></h3>
807<div class="api_metadata">
808<details class="changelog"><summary>History</summary>
809<table>
810<tbody><tr><th>Version</th><th>Changes</th></tr>
811<tr><td>v14.7.0</td>
812<td><p>This class now inherits from <code>EventTarget</code> rather than from <code>EventEmitter</code>.</p></td></tr>
813<tr><td>v10.5.0</td>
814<td><p><span>Added in: v10.5.0</span></p></td></tr>
815</tbody></table>
816</details>
817</div>
818<ul>
819<li>Extends: <a href="events.html#class-eventtarget" class="type">&#x3C;EventTarget></a></li>
820</ul>
821<p>Instances of the <code>worker.MessagePort</code> class represent one end of an
822asynchronous, two-way communications channel. It can be used to transfer
823structured data, memory regions and other <code>MessagePort</code>s between different
824<a href="#class-worker"><code>Worker</code></a>s.</p>
825<p>This implementation matches <a href="https://developer.mozilla.org/en-US/docs/Web/API/MessagePort">browser <code>MessagePort</code></a>s.</p>
826<h4>Event: <code>'close'</code><span><a class="mark" href="#event-close" id="event-close">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_event_close"></a></h4>
827<div class="api_metadata">
828<span>Added in: v10.5.0</span>
829</div>
830<p>The <code>'close'</code> event is emitted once either side of the channel has been
831disconnected.</p>
832<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
833<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
834
835<span class="hljs-comment">// Prints:</span>
836<span class="hljs-comment">//   foobar</span>
837<span class="hljs-comment">//   closed!</span>
838port2.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">message</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(message));
839port2.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'closed!'</span>));
840
841port1.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'foobar'</span>);
842port1.<span class="hljs-title function_">close</span>();</code> <button class="copy-button">copy</button></pre>
843<h4>Event: <code>'message'</code><span><a class="mark" href="#event-message" id="event-message">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_event_message"></a></h4>
844<div class="api_metadata">
845<span>Added in: v10.5.0</span>
846</div>
847<ul>
848<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> The transmitted value</li>
849</ul>
850<p>The <code>'message'</code> event is emitted for any incoming message, containing the cloned
851input of <a href="#portpostmessagevalue-transferlist"><code>port.postMessage()</code></a>.</p>
852<p>Listeners on this event receive a clone of the <code>value</code> parameter as passed
853to <code>postMessage()</code> and no further arguments.</p>
854<h4>Event: <code>'messageerror'</code><span><a class="mark" href="#event-messageerror" id="event-messageerror">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_event_messageerror"></a></h4>
855<div class="api_metadata">
856<span>Added in: v14.5.0, v12.19.0</span>
857</div>
858<ul>
859<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> An Error object</li>
860</ul>
861<p>The <code>'messageerror'</code> event is emitted when deserializing a message failed.</p>
862<p>Currently, this event is emitted when there is an error occurring while
863instantiating the posted JS object on the receiving end. Such situations
864are rare, but can happen, for instance, when certain Node.js API objects
865are received in a <code>vm.Context</code> (where Node.js APIs are currently
866unavailable).</p>
867<h4><code>port.close()</code><span><a class="mark" href="#portclose" id="portclose">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_port_close"></a></h4>
868<div class="api_metadata">
869<span>Added in: v10.5.0</span>
870</div>
871<p>Disables further sending of messages on either side of the connection.
872This method can be called when no further communication will happen over this
873<code>MessagePort</code>.</p>
874<p>The <a href="#event-close"><code>'close'</code> event</a> is emitted on both <code>MessagePort</code> instances that
875are part of the channel.</p>
876<h4><code>port.postMessage(value[, transferList])</code><span><a class="mark" href="#portpostmessagevalue-transferlist" id="portpostmessagevalue-transferlist">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_port_postmessage_value_transferlist"></a></h4>
877<div class="api_metadata">
878<details class="changelog"><summary>History</summary>
879<table>
880<tbody><tr><th>Version</th><th>Changes</th></tr>
881<tr><td>v15.6.0</td>
882<td><p>Added <code>X509Certificate</code> to the list of cloneable types.</p></td></tr>
883<tr><td>v15.0.0</td>
884<td><p>Added <code>CryptoKey</code> to the list of cloneable types.</p></td></tr>
885<tr><td>v15.14.0, v14.18.0</td>
886<td><p>Add 'BlockList' to the list of cloneable types.</p></td></tr>
887<tr><td>v15.9.0, v14.18.0</td>
888<td><p>Add 'Histogram' types to the list of cloneable types.</p></td></tr>
889<tr><td>v14.5.0, v12.19.0</td>
890<td><p>Added <code>KeyObject</code> to the list of cloneable types.</p></td></tr>
891<tr><td>v14.5.0, v12.19.0</td>
892<td><p>Added <code>FileHandle</code> to the list of transferable types.</p></td></tr>
893<tr><td>v10.5.0</td>
894<td><p><span>Added in: v10.5.0</span></p></td></tr>
895</tbody></table>
896</details>
897</div>
898<ul>
899<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
900<li><code>transferList</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object[]></a></li>
901</ul>
902<p>Sends a JavaScript value to the receiving side of this channel.
903<code>value</code> is transferred in a way which is compatible with
904the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm">HTML structured clone algorithm</a>.</p>
905<p>In particular, the significant differences to <code>JSON</code> are:</p>
906<ul>
907<li><code>value</code> may contain circular references.</li>
908<li><code>value</code> may contain instances of builtin JS types such as <code>RegExp</code>s,
909<code>BigInt</code>s, <code>Map</code>s, <code>Set</code>s, etc.</li>
910<li><code>value</code> may contain typed arrays, both using <code>ArrayBuffer</code>s
911and <code>SharedArrayBuffer</code>s.</li>
912<li><code>value</code> may contain <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module"><code>WebAssembly.Module</code></a> instances.</li>
913<li><code>value</code> may not contain native (C++-backed) objects other than:
914<ul>
915<li><a href="webcrypto.html#class-cryptokey" class="type">&#x3C;CryptoKey></a>s,</li>
916<li><a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a>s,</li>
917<li><a href="perf_hooks.html#class-histogram" class="type">&#x3C;Histogram></a>s,</li>
918<li><a href="crypto.html#class-keyobject" class="type">&#x3C;KeyObject></a>s,</li>
919<li><a href="worker_threads.html#class-messageport" class="type">&#x3C;MessagePort></a>s,</li>
920<li><a href="net.html#class-netblocklist" class="type">&#x3C;net.BlockList></a>s,</li>
921<li><a href="net.html#class-netsocketaddress" class="type">&#x3C;net.SocketAddress></a>es,</li>
922<li><a href="crypto.html#class-x509certificate" class="type">&#x3C;X509Certificate></a>s.</li>
923</ul>
924</li>
925</ul>
926<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
927<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
928
929port1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">message</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(message));
930
931<span class="hljs-keyword">const</span> circularData = {};
932circularData.<span class="hljs-property">foo</span> = circularData;
933<span class="hljs-comment">// Prints: { foo: [Circular] }</span>
934port2.<span class="hljs-title function_">postMessage</span>(circularData);</code> <button class="copy-button">copy</button></pre>
935<p><code>transferList</code> may be a list of <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>, <a href="#class-messageport"><code>MessagePort</code></a>, and
936<a href="fs.html#class-filehandle"><code>FileHandle</code></a> objects.
937After transferring, they are not usable on the sending side of the channel
938anymore (even if they are not contained in <code>value</code>). Unlike with
939<a href="child_process.html">child processes</a>, transferring handles such as network sockets is currently
940not supported.</p>
941<p>If <code>value</code> contains <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> instances, those are accessible
942from either thread. They cannot be listed in <code>transferList</code>.</p>
943<p><code>value</code> may still contain <code>ArrayBuffer</code> instances that are not in
944<code>transferList</code>; in that case, the underlying memory is copied rather than moved.</p>
945<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
946<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
947
948port1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">message</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(message));
949
950<span class="hljs-keyword">const</span> uint8Array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>([ <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span> ]);
951<span class="hljs-comment">// This posts a copy of `uint8Array`:</span>
952port2.<span class="hljs-title function_">postMessage</span>(uint8Array);
953<span class="hljs-comment">// This does not copy data, but renders `uint8Array` unusable:</span>
954port2.<span class="hljs-title function_">postMessage</span>(uint8Array, [ uint8Array.<span class="hljs-property">buffer</span> ]);
955
956<span class="hljs-comment">// The memory for the `sharedUint8Array` is accessible from both the</span>
957<span class="hljs-comment">// original and the copy received by `.on('message')`:</span>
958<span class="hljs-keyword">const</span> sharedUint8Array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">SharedArrayBuffer</span>(<span class="hljs-number">4</span>));
959port2.<span class="hljs-title function_">postMessage</span>(sharedUint8Array);
960
961<span class="hljs-comment">// This transfers a freshly created message port to the receiver.</span>
962<span class="hljs-comment">// This can be used, for example, to create communication channels between</span>
963<span class="hljs-comment">// multiple `Worker` threads that are children of the same parent thread.</span>
964<span class="hljs-keyword">const</span> otherChannel = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
965port2.<span class="hljs-title function_">postMessage</span>({ <span class="hljs-attr">port</span>: otherChannel.<span class="hljs-property">port1</span> }, [ otherChannel.<span class="hljs-property">port1</span> ]);</code> <button class="copy-button">copy</button></pre>
966<p>The message object is cloned immediately, and can be modified after
967posting without having side effects.</p>
968<p>For more information on the serialization and deserialization mechanisms
969behind this API, see the <a href="v8.html#serialization-api">serialization API of the <code>node:v8</code> module</a>.</p>
970<h5>Considerations when transferring TypedArrays and Buffers<span><a class="mark" href="#considerations-when-transferring-typedarrays-and-buffers" id="considerations-when-transferring-typedarrays-and-buffers">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_considerations_when_transferring_typedarrays_and_buffers"></a></h5>
971<p>All <code>TypedArray</code> and <code>Buffer</code> instances are views over an underlying
972<code>ArrayBuffer</code>. That is, it is the <code>ArrayBuffer</code> that actually stores
973the raw data while the <code>TypedArray</code> and <code>Buffer</code> objects provide a
974way of viewing and manipulating the data. It is possible and common
975for multiple views to be created over the same <code>ArrayBuffer</code> instance.
976Great care must be taken when using a transfer list to transfer an
977<code>ArrayBuffer</code> as doing so causes all <code>TypedArray</code> and <code>Buffer</code>
978instances that share that same <code>ArrayBuffer</code> to become unusable.</p>
979<pre><code class="language-js"><span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">10</span>);
980
981<span class="hljs-keyword">const</span> u1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(ab);
982<span class="hljs-keyword">const</span> u2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(ab);
983
984<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(u2.<span class="hljs-property">length</span>);  <span class="hljs-comment">// prints 5</span>
985
986port.<span class="hljs-title function_">postMessage</span>(u1, [u1.<span class="hljs-property">buffer</span>]);
987
988<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(u2.<span class="hljs-property">length</span>);  <span class="hljs-comment">// prints 0</span></code> <button class="copy-button">copy</button></pre>
989<p>For <code>Buffer</code> instances, specifically, whether the underlying
990<code>ArrayBuffer</code> can be transferred or cloned depends entirely on how
991instances were created, which often cannot be reliably determined.</p>
992<p>An <code>ArrayBuffer</code> can be marked with <a href="#workermarkasuntransferableobject"><code>markAsUntransferable()</code></a> to indicate
993that it should always be cloned and never transferred.</p>
994<p>Depending on how a <code>Buffer</code> instance was created, it may or may
995not own its underlying <code>ArrayBuffer</code>. An <code>ArrayBuffer</code> must not
996be transferred unless it is known that the <code>Buffer</code> instance
997owns it. In particular, for <code>Buffer</code>s created from the internal
998<code>Buffer</code> pool (using, for instance <code>Buffer.from()</code> or <code>Buffer.allocUnsafe()</code>),
999transferring them is not possible and they are always cloned,
1000which sends a copy of the entire <code>Buffer</code> pool.
1001This behavior may come with unintended higher memory
1002usage and possible security concerns.</p>
1003<p>See <a href="buffer.html#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> for more details on <code>Buffer</code> pooling.</p>
1004<p>The <code>ArrayBuffer</code>s for <code>Buffer</code> instances created using
1005<code>Buffer.alloc()</code> or <code>Buffer.allocUnsafeSlow()</code> can always be
1006transferred but doing so renders all other existing views of
1007those <code>ArrayBuffer</code>s unusable.</p>
1008<h5>Considerations when cloning objects with prototypes, classes, and accessors<span><a class="mark" href="#considerations-when-cloning-objects-with-prototypes-classes-and-accessors" id="considerations-when-cloning-objects-with-prototypes-classes-and-accessors">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_considerations_when_cloning_objects_with_prototypes_classes_and_accessors"></a></h5>
1009<p>Because object cloning uses the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm">HTML structured clone algorithm</a>,
1010non-enumerable properties, property accessors, and object prototypes are
1011not preserved. In particular, <a href="buffer.html"><code>Buffer</code></a> objects will be read as
1012plain <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a>s on the receiving side, and instances of JavaScript
1013classes will be cloned as plain JavaScript objects.</p>
1014<pre><code class="language-js"><span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Symbol</span>(<span class="hljs-string">'b'</span>);
1015
1016<span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> {
1017  #a = <span class="hljs-number">1</span>;
1018  <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) {
1019    <span class="hljs-variable language_">this</span>[b] = <span class="hljs-number">2</span>;
1020    <span class="hljs-variable language_">this</span>.<span class="hljs-property">c</span> = <span class="hljs-number">3</span>;
1021  }
1022
1023  <span class="hljs-keyword">get</span> <span class="hljs-title function_">d</span>() { <span class="hljs-keyword">return</span> <span class="hljs-number">4</span>; }
1024}
1025
1026<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
1027
1028port1.<span class="hljs-property">onmessage</span> = <span class="hljs-function">(<span class="hljs-params">{ data }</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data);
1029
1030port2.<span class="hljs-title function_">postMessage</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Foo</span>());
1031
1032<span class="hljs-comment">// Prints: { c: 3 }</span></code> <button class="copy-button">copy</button></pre>
1033<p>This limitation extends to many built-in objects, such as the global <code>URL</code>
1034object:</p>
1035<pre><code class="language-js"><span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
1036
1037port1.<span class="hljs-property">onmessage</span> = <span class="hljs-function">(<span class="hljs-params">{ data }</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data);
1038
1039port2.<span class="hljs-title function_">postMessage</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org'</span>));
1040
1041<span class="hljs-comment">// Prints: { }</span></code> <button class="copy-button">copy</button></pre>
1042<h4><code>port.hasRef()</code><span><a class="mark" href="#porthasref" id="porthasref">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_port_hasref"></a></h4>
1043<div class="api_metadata">
1044<span>Added in: v18.1.0</span>
1045</div>
1046<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
1047<ul>
1048<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1049</ul>
1050<p>If true, the <code>MessagePort</code> object will keep the Node.js event loop active.</p>
1051<h4><code>port.ref()</code><span><a class="mark" href="#portref" id="portref">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_port_ref"></a></h4>
1052<div class="api_metadata">
1053<span>Added in: v10.5.0</span>
1054</div>
1055<p>Opposite of <code>unref()</code>. Calling <code>ref()</code> on a previously <code>unref()</code>ed port does
1056<em>not</em> let the program exit if it's the only active handle left (the default
1057behavior). If the port is <code>ref()</code>ed, calling <code>ref()</code> again has no effect.</p>
1058<p>If listeners are attached or removed using <code>.on('message')</code>, the port
1059is <code>ref()</code>ed and <code>unref()</code>ed automatically depending on whether
1060listeners for the event exist.</p>
1061<h4><code>port.start()</code><span><a class="mark" href="#portstart" id="portstart">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_port_start"></a></h4>
1062<div class="api_metadata">
1063<span>Added in: v10.5.0</span>
1064</div>
1065<p>Starts receiving messages on this <code>MessagePort</code>. When using this port
1066as an event emitter, this is called automatically once <code>'message'</code>
1067listeners are attached.</p>
1068<p>This method exists for parity with the Web <code>MessagePort</code> API. In Node.js,
1069it is only useful for ignoring messages when no event listener is present.
1070Node.js also diverges in its handling of <code>.onmessage</code>. Setting it
1071automatically calls <code>.start()</code>, but unsetting it lets messages queue up
1072until a new handler is set or the port is discarded.</p>
1073<h4><code>port.unref()</code><span><a class="mark" href="#portunref" id="portunref">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_port_unref"></a></h4>
1074<div class="api_metadata">
1075<span>Added in: v10.5.0</span>
1076</div>
1077<p>Calling <code>unref()</code> on a port allows the thread to exit if this is the only
1078active handle in the event system. If the port is already <code>unref()</code>ed calling
1079<code>unref()</code> again has no effect.</p>
1080<p>If listeners are attached or removed using <code>.on('message')</code>, the port is
1081<code>ref()</code>ed and <code>unref()</code>ed automatically depending on whether
1082listeners for the event exist.</p>
1083</section><section><h3>Class: <code>Worker</code><span><a class="mark" href="#class-worker" id="class-worker">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_class_worker"></a></h3>
1084<div class="api_metadata">
1085<span>Added in: v10.5.0</span>
1086</div>
1087<ul>
1088<li>Extends: <a href="events.html#class-eventemitter" class="type">&#x3C;EventEmitter></a></li>
1089</ul>
1090<p>The <code>Worker</code> class represents an independent JavaScript execution thread.
1091Most Node.js APIs are available inside of it.</p>
1092<p>Notable differences inside a Worker environment are:</p>
1093<ul>
1094<li>The <a href="process.html#processstdin"><code>process.stdin</code></a>, <a href="process.html#processstdout"><code>process.stdout</code></a>, and <a href="process.html#processstderr"><code>process.stderr</code></a>
1095streams may be redirected by the parent thread.</li>
1096<li>The <a href="#workerismainthread"><code>require('node:worker_threads').isMainThread</code></a> property is set to <code>false</code>.</li>
1097<li>The <a href="#workerparentport"><code>require('node:worker_threads').parentPort</code></a> message port is available.</li>
1098<li><a href="process.html#processexitcode"><code>process.exit()</code></a> does not stop the whole program, just the single thread,
1099and <a href="process.html#processabort"><code>process.abort()</code></a> is not available.</li>
1100<li><a href="process.html#processchdirdirectory"><code>process.chdir()</code></a> and <code>process</code> methods that set group or user ids
1101are not available.</li>
1102<li><a href="process.html#processenv"><code>process.env</code></a> is a copy of the parent thread's environment variables,
1103unless otherwise specified. Changes to one copy are not visible in other
1104threads, and are not visible to native add-ons (unless
1105<a href="#workershare_env"><code>worker.SHARE_ENV</code></a> is passed as the <code>env</code> option to the
1106<a href="#class-worker"><code>Worker</code></a> constructor). On Windows, unlike the main thread, a copy of the
1107environment variables operates in a case-sensitive manner.</li>
1108<li><a href="process.html#processtitle"><code>process.title</code></a> cannot be modified.</li>
1109<li>Signals are not delivered through <a href="process.html#signal-events"><code>process.on('...')</code></a>.</li>
1110<li>Execution may stop at any point as a result of <a href="#workerterminate"><code>worker.terminate()</code></a>
1111being invoked.</li>
1112<li>IPC channels from parent processes are not accessible.</li>
1113<li>The <a href="tracing.html"><code>trace_events</code></a> module is not supported.</li>
1114<li>Native add-ons can only be loaded from multiple threads if they fulfill
1115<a href="addons.html#worker-support">certain conditions</a>.</li>
1116</ul>
1117<p>Creating <code>Worker</code> instances inside of other <code>Worker</code>s is possible.</p>
1118<p>Like <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API">Web Workers</a> and the <a href="cluster.html"><code>node:cluster</code> module</a>, two-way communication
1119can be achieved through inter-thread message passing. Internally, a <code>Worker</code> has
1120a built-in pair of <a href="#class-messageport"><code>MessagePort</code></a>s that are already associated with each
1121other when the <code>Worker</code> is created. While the <code>MessagePort</code> object on the parent
1122side is not directly exposed, its functionalities are exposed through
1123<a href="#workerpostmessagevalue-transferlist"><code>worker.postMessage()</code></a> and the <a href="#event-message_1"><code>worker.on('message')</code></a> event
1124on the <code>Worker</code> object for the parent thread.</p>
1125<p>To create custom messaging channels (which is encouraged over using the default
1126global channel because it facilitates separation of concerns), users can create
1127a <code>MessageChannel</code> object on either thread and pass one of the
1128<code>MessagePort</code>s on that <code>MessageChannel</code> to the other thread through a
1129pre-existing channel, such as the global one.</p>
1130<p>See <a href="#portpostmessagevalue-transferlist"><code>port.postMessage()</code></a> for more information on how messages are passed,
1131and what kind of JavaScript values can be successfully transported through
1132the thread barrier.</p>
1133<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
1134<span class="hljs-keyword">const</span> {
1135  <span class="hljs-title class_">Worker</span>, <span class="hljs-title class_">MessageChannel</span>, <span class="hljs-title class_">MessagePort</span>, isMainThread, parentPort,
1136} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
1137<span class="hljs-keyword">if</span> (isMainThread) {
1138  <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename);
1139  <span class="hljs-keyword">const</span> subChannel = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
1140  worker.<span class="hljs-title function_">postMessage</span>({ <span class="hljs-attr">hereIsYourPort</span>: subChannel.<span class="hljs-property">port1</span> }, [subChannel.<span class="hljs-property">port1</span>]);
1141  subChannel.<span class="hljs-property">port2</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">value</span>) =></span> {
1142    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'received:'</span>, value);
1143  });
1144} <span class="hljs-keyword">else</span> {
1145  parentPort.<span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">value</span>) =></span> {
1146    <span class="hljs-title function_">assert</span>(value.<span class="hljs-property">hereIsYourPort</span> <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">MessagePort</span>);
1147    value.<span class="hljs-property">hereIsYourPort</span>.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'the worker is sending this'</span>);
1148    value.<span class="hljs-property">hereIsYourPort</span>.<span class="hljs-title function_">close</span>();
1149  });
1150}</code> <button class="copy-button">copy</button></pre>
1151<h4><code>new Worker(filename[, options])</code><span><a class="mark" href="#new-workerfilename-options" id="new-workerfilename-options">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_new_worker_filename_options"></a></h4>
1152<div class="api_metadata">
1153<details class="changelog"><summary>History</summary>
1154<table>
1155<tbody><tr><th>Version</th><th>Changes</th></tr>
1156<tr><td>v18.16.0</td>
1157<td><p>Added support for a <code>name</code> option, which allows adding a name to worker title for debugging.</p></td></tr>
1158<tr><td>v14.9.0</td>
1159<td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>data:</code> protocol.</p></td></tr>
1160<tr><td>v14.9.0</td>
1161<td><p>The <code>trackUnmanagedFds</code> option was set to <code>true</code> by default.</p></td></tr>
1162<tr><td>v14.6.0, v12.19.0</td>
1163<td><p>The <code>trackUnmanagedFds</code> option was introduced.</p></td></tr>
1164<tr><td>v13.13.0, v12.17.0</td>
1165<td><p>The <code>transferList</code> option was introduced.</p></td></tr>
1166<tr><td>v13.12.0, v12.17.0</td>
1167<td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
1168<tr><td>v13.4.0, v12.16.0</td>
1169<td><p>The <code>argv</code> option was introduced.</p></td></tr>
1170<tr><td>v13.2.0, v12.16.0</td>
1171<td><p>The <code>resourceLimits</code> option was introduced.</p></td></tr>
1172<tr><td>v10.5.0</td>
1173<td><p><span>Added in: v10.5.0</span></p></td></tr>
1174</tbody></table>
1175</details>
1176</div>
1177<ul>
1178<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> The path to the Worker's main script or module. Must
1179be either an absolute path or a relative path (i.e. relative to the
1180current working directory) starting with <code>./</code> or <code>../</code>, or a WHATWG <code>URL</code>
1181object using <code>file:</code> or <code>data:</code> protocol.
1182When using a <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs"><code>data:</code> URL</a>, the data is interpreted based on MIME type using
1183the <a href="esm.html#data-imports">ECMAScript module loader</a>.
1184If <code>options.eval</code> is <code>true</code>, this is a string containing JavaScript code
1185rather than a path.</li>
1186<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1187<ul>
1188<li><code>argv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any[]></a> List of arguments which would be stringified and appended to
1189<code>process.argv</code> in the worker. This is mostly similar to the <code>workerData</code>
1190but the values are available on the global <code>process.argv</code> as if they
1191were passed as CLI options to the script.</li>
1192<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> If set, specifies the initial value of <code>process.env</code> inside
1193the Worker thread. As a special value, <a href="#workershare_env"><code>worker.SHARE_ENV</code></a> may be used
1194to specify that the parent thread and the child thread should share their
1195environment variables; in that case, changes to one thread's <code>process.env</code>
1196object affect the other thread as well. <strong>Default:</strong> <code>process.env</code>.</li>
1197<li><code>eval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code> and the first argument is a <code>string</code>, interpret
1198the first argument to the constructor as a script that is executed once the
1199worker is online.</li>
1200<li><code>execArgv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> List of node CLI options passed to the worker.
1201V8 options (such as <code>--max-old-space-size</code>) and options that affect the
1202process (such as <code>--title</code>) are not supported. If set, this is provided
1203as <a href="process.html#processexecargv"><code>process.execArgv</code></a> inside the worker. By default, options are
1204inherited from the parent thread.</li>
1205<li><code>stdin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If this is set to <code>true</code>, then <code>worker.stdin</code>
1206provides a writable stream whose contents appear as <code>process.stdin</code>
1207inside the Worker. By default, no data is provided.</li>
1208<li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If this is set to <code>true</code>, then <code>worker.stdout</code> is
1209not automatically piped through to <code>process.stdout</code> in the parent.</li>
1210<li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If this is set to <code>true</code>, then <code>worker.stderr</code> is
1211not automatically piped through to <code>process.stderr</code> in the parent.</li>
1212<li><code>workerData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> Any JavaScript value that is cloned and made
1213available as <a href="#workerworkerdata"><code>require('node:worker_threads').workerData</code></a>. The cloning
1214occurs as described in the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm">HTML structured clone algorithm</a>, and an error
1215is thrown if the object cannot be cloned (e.g. because it contains
1216<code>function</code>s).</li>
1217<li><code>trackUnmanagedFds</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If this is set to <code>true</code>, then the Worker
1218tracks raw file descriptors managed through <a href="fs.html#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> and
1219<a href="fs.html#fsclosefd-callback"><code>fs.close()</code></a>, and closes them when the Worker exits, similar to other
1220resources like network sockets or file descriptors managed through
1221the <a href="fs.html#class-filehandle"><code>FileHandle</code></a> API. This option is automatically inherited by all
1222nested <code>Worker</code>s. <strong>Default:</strong> <code>true</code>.</li>
1223<li><code>transferList</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object[]></a> If one or more <code>MessagePort</code>-like objects
1224are passed in <code>workerData</code>, a <code>transferList</code> is required for those
1225items or <a href="errors.html#err_missing_message_port_in_transfer_list"><code>ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST</code></a> is thrown.
1226See <a href="#portpostmessagevalue-transferlist"><code>port.postMessage()</code></a> for more information.</li>
1227<li><code>resourceLimits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> An optional set of resource limits for the new JS
1228engine instance. Reaching these limits leads to termination of the <code>Worker</code>
1229instance. These limits only affect the JS engine, and no external data,
1230including no <code>ArrayBuffer</code>s. Even if these limits are set, the process may
1231still abort if it encounters a global out-of-memory situation.
1232<ul>
1233<li><code>maxOldGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The maximum size of the main heap in
1234MB. If the command-line argument <a href="cli.html#--max-old-space-sizesize-in-megabytes"><code>--max-old-space-size</code></a> is set, it
1235overrides this setting.</li>
1236<li><code>maxYoungGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The maximum size of a heap space for
1237recently created objects. If the command-line argument
1238<a href="cli.html#--max-semi-space-sizesize-in-megabytes"><code>--max-semi-space-size</code></a> is set, it overrides this setting.</li>
1239<li><code>codeRangeSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The size of a pre-allocated memory range
1240used for generated code.</li>
1241<li><code>stackSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The default maximum stack size for the thread.
1242Small values may lead to unusable Worker instances. <strong>Default:</strong> <code>4</code>.</li>
1243</ul>
1244</li>
1245<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> An optional <code>name</code> to be appended to the worker title
1246for debugging/identification purposes, making the final title as
1247<code>[worker ${id}] ${name}</code>. <strong>Default:</strong> <code>''</code>.</li>
1248</ul>
1249</li>
1250</ul>
1251<h4>Event: <code>'error'</code><span><a class="mark" href="#event-error" id="event-error">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_event_error"></a></h4>
1252<div class="api_metadata">
1253<span>Added in: v10.5.0</span>
1254</div>
1255<ul>
1256<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
1257</ul>
1258<p>The <code>'error'</code> event is emitted if the worker thread throws an uncaught
1259exception. In that case, the worker is terminated.</p>
1260<h4>Event: <code>'exit'</code><span><a class="mark" href="#event-exit" id="event-exit">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_event_exit"></a></h4>
1261<div class="api_metadata">
1262<span>Added in: v10.5.0</span>
1263</div>
1264<ul>
1265<li><code>exitCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1266</ul>
1267<p>The <code>'exit'</code> event is emitted once the worker has stopped. If the worker
1268exited by calling <a href="process.html#processexitcode"><code>process.exit()</code></a>, the <code>exitCode</code> parameter is the
1269passed exit code. If the worker was terminated, the <code>exitCode</code> parameter is
1270<code>1</code>.</p>
1271<p>This is the final event emitted by any <code>Worker</code> instance.</p>
1272<h4>Event: <code>'message'</code><span><a class="mark" href="#event-message_1" id="event-message_1">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_event_message_1"></a></h4>
1273<div class="api_metadata">
1274<span>Added in: v10.5.0</span>
1275</div>
1276<ul>
1277<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> The transmitted value</li>
1278</ul>
1279<p>The <code>'message'</code> event is emitted when the worker thread has invoked
1280<a href="#workerpostmessagevalue-transferlist"><code>require('node:worker_threads').parentPort.postMessage()</code></a>.
1281See the <a href="#event-message"><code>port.on('message')</code></a> event for more details.</p>
1282<p>All messages sent from the worker thread are emitted before the
1283<a href="#event-exit"><code>'exit'</code> event</a> is emitted on the <code>Worker</code> object.</p>
1284<h4>Event: <code>'messageerror'</code><span><a class="mark" href="#event-messageerror_1" id="event-messageerror_1">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_event_messageerror_1"></a></h4>
1285<div class="api_metadata">
1286<span>Added in: v14.5.0, v12.19.0</span>
1287</div>
1288<ul>
1289<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> An Error object</li>
1290</ul>
1291<p>The <code>'messageerror'</code> event is emitted when deserializing a message failed.</p>
1292<h4>Event: <code>'online'</code><span><a class="mark" href="#event-online" id="event-online">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_event_online"></a></h4>
1293<div class="api_metadata">
1294<span>Added in: v10.5.0</span>
1295</div>
1296<p>The <code>'online'</code> event is emitted when the worker thread has started executing
1297JavaScript code.</p>
1298<h4><code>worker.getHeapSnapshot()</code><span><a class="mark" href="#workergetheapsnapshot" id="workergetheapsnapshot">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_getheapsnapshot"></a></h4>
1299<div class="api_metadata">
1300<span>Added in: v13.9.0, v12.17.0</span>
1301</div>
1302<ul>
1303<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> A promise for a Readable Stream containing
1304a V8 heap snapshot</li>
1305</ul>
1306<p>Returns a readable stream for a V8 snapshot of the current state of the Worker.
1307See <a href="v8.html#v8getheapsnapshot"><code>v8.getHeapSnapshot()</code></a> for more details.</p>
1308<p>If the Worker thread is no longer running, which may occur before the
1309<a href="#event-exit"><code>'exit'</code> event</a> is emitted, the returned <code>Promise</code> is rejected
1310immediately with an <a href="errors.html#err_worker_not_running"><code>ERR_WORKER_NOT_RUNNING</code></a> error.</p>
1311<h4><code>worker.performance</code><span><a class="mark" href="#workerperformance" id="workerperformance">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_performance"></a></h4>
1312<div class="api_metadata">
1313<span>Added in: v15.1.0, v14.17.0, v12.22.0</span>
1314</div>
1315<p>An object that can be used to query performance information from a worker
1316instance. Similar to <a href="perf_hooks.html#perf_hooksperformance"><code>perf_hooks.performance</code></a>.</p>
1317<h5><code>performance.eventLoopUtilization([utilization1[, utilization2]])</code><span><a class="mark" href="#performanceeventlooputilizationutilization1-utilization2" id="performanceeventlooputilizationutilization1-utilization2">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_performance_eventlooputilization_utilization1_utilization2"></a></h5>
1318<div class="api_metadata">
1319<span>Added in: v15.1.0, v14.17.0, v12.22.0</span>
1320</div>
1321<ul>
1322<li><code>utilization1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The result of a previous call to
1323<code>eventLoopUtilization()</code>.</li>
1324<li><code>utilization2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The result of a previous call to
1325<code>eventLoopUtilization()</code> prior to <code>utilization1</code>.</li>
1326<li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1327<ul>
1328<li><code>idle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1329<li><code>active</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1330<li><code>utilization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1331</ul>
1332</li>
1333</ul>
1334<p>The same call as <a href="perf_hooks.html#performanceeventlooputilizationutilization1-utilization2"><code>perf_hooks</code> <code>eventLoopUtilization()</code></a>, except the values
1335of the worker instance are returned.</p>
1336<p>One difference is that, unlike the main thread, bootstrapping within a worker
1337is done within the event loop. So the event loop utilization is
1338immediately available once the worker's script begins execution.</p>
1339<p>An <code>idle</code> time that does not increase does not indicate that the worker is
1340stuck in bootstrap. The following examples shows how the worker's entire
1341lifetime never accumulates any <code>idle</code> time, but is still be able to process
1342messages.</p>
1343<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMainThread, parentPort } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
1344
1345<span class="hljs-keyword">if</span> (isMainThread) {
1346  <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename);
1347  <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
1348    worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'hi'</span>);
1349    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(worker.<span class="hljs-property">performance</span>.<span class="hljs-title function_">eventLoopUtilization</span>());
1350  }, <span class="hljs-number">100</span>).<span class="hljs-title function_">unref</span>();
1351  <span class="hljs-keyword">return</span>;
1352}
1353
1354parentPort.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'msg'</span>)).<span class="hljs-title function_">unref</span>();
1355(<span class="hljs-keyword">function</span> <span class="hljs-title function_">r</span>(<span class="hljs-params">n</span>) {
1356  <span class="hljs-keyword">if</span> (--n &#x3C; <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span>;
1357  <span class="hljs-keyword">const</span> t = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>();
1358  <span class="hljs-keyword">while</span> (<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>() - t &#x3C; <span class="hljs-number">300</span>);
1359  <span class="hljs-title function_">setImmediate</span>(r, n);
1360})(<span class="hljs-number">10</span>);</code> <button class="copy-button">copy</button></pre>
1361<p>The event loop utilization of a worker is available only after the <a href="#event-online"><code>'online'</code>
1362event</a> emitted, and if called before this, or after the <a href="#event-exit"><code>'exit'</code>
1363event</a>, then all properties have the value of <code>0</code>.</p>
1364<h4><code>worker.postMessage(value[, transferList])</code><span><a class="mark" href="#workerpostmessagevalue-transferlist" id="workerpostmessagevalue-transferlist">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_postmessage_value_transferlist"></a></h4>
1365<div class="api_metadata">
1366<span>Added in: v10.5.0</span>
1367</div>
1368<ul>
1369<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1370<li><code>transferList</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object[]></a></li>
1371</ul>
1372<p>Send a message to the worker that is received via
1373<a href="#event-message"><code>require('node:worker_threads').parentPort.on('message')</code></a>.
1374See <a href="#portpostmessagevalue-transferlist"><code>port.postMessage()</code></a> for more details.</p>
1375<h4><code>worker.ref()</code><span><a class="mark" href="#workerref" id="workerref">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_ref"></a></h4>
1376<div class="api_metadata">
1377<span>Added in: v10.5.0</span>
1378</div>
1379<p>Opposite of <code>unref()</code>, calling <code>ref()</code> on a previously <code>unref()</code>ed worker does
1380<em>not</em> let the program exit if it's the only active handle left (the default
1381behavior). If the worker is <code>ref()</code>ed, calling <code>ref()</code> again has
1382no effect.</p>
1383<h4><code>worker.resourceLimits</code><span><a class="mark" href="#workerresourcelimits_1" id="workerresourcelimits_1">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_resourcelimits_1"></a></h4>
1384<div class="api_metadata">
1385<span>Added in: v13.2.0, v12.16.0</span>
1386</div>
1387<ul>
1388<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1389<ul>
1390<li><code>maxYoungGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1391<li><code>maxOldGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1392<li><code>codeRangeSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1393<li><code>stackSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1394</ul>
1395</li>
1396</ul>
1397<p>Provides the set of JS engine resource constraints for this Worker thread.
1398If the <code>resourceLimits</code> option was passed to the <a href="#class-worker"><code>Worker</code></a> constructor,
1399this matches its values.</p>
1400<p>If the worker has stopped, the return value is an empty object.</p>
1401<h4><code>worker.stderr</code><span><a class="mark" href="#workerstderr" id="workerstderr">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_stderr"></a></h4>
1402<div class="api_metadata">
1403<span>Added in: v10.5.0</span>
1404</div>
1405<ul>
1406<li><a href="stream.html#class-streamreadable" class="type">&#x3C;stream.Readable></a></li>
1407</ul>
1408<p>This is a readable stream which contains data written to <a href="process.html#processstderr"><code>process.stderr</code></a>
1409inside the worker thread. If <code>stderr: true</code> was not passed to the
1410<a href="#class-worker"><code>Worker</code></a> constructor, then data is piped to the parent thread's
1411<a href="process.html#processstderr"><code>process.stderr</code></a> stream.</p>
1412<h4><code>worker.stdin</code><span><a class="mark" href="#workerstdin" id="workerstdin">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_stdin"></a></h4>
1413<div class="api_metadata">
1414<span>Added in: v10.5.0</span>
1415</div>
1416<ul>
1417<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> | <a href="stream.html#class-streamwritable" class="type">&#x3C;stream.Writable></a></li>
1418</ul>
1419<p>If <code>stdin: true</code> was passed to the <a href="#class-worker"><code>Worker</code></a> constructor, this is a
1420writable stream. The data written to this stream will be made available in
1421the worker thread as <a href="process.html#processstdin"><code>process.stdin</code></a>.</p>
1422<h4><code>worker.stdout</code><span><a class="mark" href="#workerstdout" id="workerstdout">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_stdout"></a></h4>
1423<div class="api_metadata">
1424<span>Added in: v10.5.0</span>
1425</div>
1426<ul>
1427<li><a href="stream.html#class-streamreadable" class="type">&#x3C;stream.Readable></a></li>
1428</ul>
1429<p>This is a readable stream which contains data written to <a href="process.html#processstdout"><code>process.stdout</code></a>
1430inside the worker thread. If <code>stdout: true</code> was not passed to the
1431<a href="#class-worker"><code>Worker</code></a> constructor, then data is piped to the parent thread's
1432<a href="process.html#processstdout"><code>process.stdout</code></a> stream.</p>
1433<h4><code>worker.terminate()</code><span><a class="mark" href="#workerterminate" id="workerterminate">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_terminate"></a></h4>
1434<div class="api_metadata">
1435<details class="changelog"><summary>History</summary>
1436<table>
1437<tbody><tr><th>Version</th><th>Changes</th></tr>
1438<tr><td>v12.5.0</td>
1439<td><p>This function now returns a Promise. Passing a callback is deprecated, and was useless up to this version, as the Worker was actually terminated synchronously. Terminating is now a fully asynchronous operation.</p></td></tr>
1440<tr><td>v10.5.0</td>
1441<td><p><span>Added in: v10.5.0</span></p></td></tr>
1442</tbody></table>
1443</details>
1444</div>
1445<ul>
1446<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
1447</ul>
1448<p>Stop all JavaScript execution in the worker thread as soon as possible.
1449Returns a Promise for the exit code that is fulfilled when the
1450<a href="#event-exit"><code>'exit'</code> event</a> is emitted.</p>
1451<h4><code>worker.threadId</code><span><a class="mark" href="#workerthreadid_1" id="workerthreadid_1">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_threadid_1"></a></h4>
1452<div class="api_metadata">
1453<span>Added in: v10.5.0</span>
1454</div>
1455<ul>
1456<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1457</ul>
1458<p>An integer identifier for the referenced thread. Inside the worker thread,
1459it is available as <a href="#workerthreadid"><code>require('node:worker_threads').threadId</code></a>.
1460This value is unique for each <code>Worker</code> instance inside a single process.</p>
1461<h4><code>worker.unref()</code><span><a class="mark" href="#workerunref" id="workerunref">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_worker_unref"></a></h4>
1462<div class="api_metadata">
1463<span>Added in: v10.5.0</span>
1464</div>
1465<p>Calling <code>unref()</code> on a worker allows the thread to exit if this is the only
1466active handle in the event system. If the worker is already <code>unref()</code>ed calling
1467<code>unref()</code> again has no effect.</p>
1468</section><section><h3>Notes<span><a class="mark" href="#notes" id="notes">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_notes"></a></h3>
1469<h4>Synchronous blocking of stdio<span><a class="mark" href="#synchronous-blocking-of-stdio" id="synchronous-blocking-of-stdio">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_synchronous_blocking_of_stdio"></a></h4>
1470<p><code>Worker</code>s utilize message passing via <a href="worker_threads.html#class-messageport" class="type">&#x3C;MessagePort></a> to implement interactions
1471with <code>stdio</code>. This means that <code>stdio</code> output originating from a <code>Worker</code> can
1472get blocked by synchronous code on the receiving end that is blocking the
1473Node.js event loop.</p>
1474
1475<pre class="with-13-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> {
1476  <span class="hljs-title class_">Worker</span>,
1477  isMainThread,
1478} <span class="hljs-keyword">from</span> <span class="hljs-string">'worker_threads'</span>;
1479
1480<span class="hljs-keyword">if</span> (isMainThread) {
1481  <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-keyword">import</span>.<span class="hljs-property">meta</span>.<span class="hljs-property">url</span>));
1482  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-number">0</span>; n &#x3C; <span class="hljs-number">1e10</span>; n++) {
1483    <span class="hljs-comment">// Looping to simulate work.</span>
1484  }
1485} <span class="hljs-keyword">else</span> {
1486  <span class="hljs-comment">// This output will be blocked by the for loop in the main thread.</span>
1487  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'foo'</span>);
1488}</code><code class="language-js cjs"><span class="hljs-meta">'use strict'</span>;
1489
1490<span class="hljs-keyword">const</span> {
1491  <span class="hljs-title class_">Worker</span>,
1492  isMainThread,
1493} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>);
1494
1495<span class="hljs-keyword">if</span> (isMainThread) {
1496  <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename);
1497  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-number">0</span>; n &#x3C; <span class="hljs-number">1e10</span>; n++) {
1498    <span class="hljs-comment">// Looping to simulate work.</span>
1499  }
1500} <span class="hljs-keyword">else</span> {
1501  <span class="hljs-comment">// This output will be blocked by the for loop in the main thread.</span>
1502  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'foo'</span>);
1503}</code><button class="copy-button">copy</button></pre>
1504<h4>Launching worker threads from preload scripts<span><a class="mark" href="#launching-worker-threads-from-preload-scripts" id="launching-worker-threads-from-preload-scripts">#</a></span><a aria-hidden="true" class="legacy" id="worker_threads_launching_worker_threads_from_preload_scripts"></a></h4>
1505<p>Take care when launching worker threads from preload scripts (scripts loaded
1506and run using the <code>-r</code> command line flag). Unless the <code>execArgv</code> option is
1507explicitly set, new Worker threads automatically inherit the command line flags
1508from the running process and will preload the same preload scripts as the main
1509thread. If the preload script unconditionally launches a worker thread, every
1510thread spawned will spawn another until the application crashes.</p></section>
1511        <!-- API END -->
1512      </div>
1513    </div>
1514  </div>
1515</body>
1516</html>
1517