11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<html lang="en"> 31cb0ef41Sopenharmony_ci<head> 41cb0ef41Sopenharmony_ci <meta charset="utf-8"> 51cb0ef41Sopenharmony_ci <meta name="viewport" content="width=device-width"> 61cb0ef41Sopenharmony_ci <meta name="nodejs.org:node-version" content="v18.20.1"> 71cb0ef41Sopenharmony_ci <title>Worker threads | Node.js v18.20.1 Documentation</title> 81cb0ef41Sopenharmony_ci <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 91cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/style.css"> 101cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/hljs.css"> 111cb0ef41Sopenharmony_ci <link rel="canonical" href="https://nodejs.org/api/worker_threads.html"> 121cb0ef41Sopenharmony_ci <script async defer src="assets/api.js" type="text/javascript"></script> 131cb0ef41Sopenharmony_ci <style>@media(max-width:326px){.with-13-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 141cb0ef41Sopenharmony_ci</head> 151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-worker_threads"> 161cb0ef41Sopenharmony_ci <div id="content" class="clearfix"> 171cb0ef41Sopenharmony_ci <div id="column2" class="interior"> 181cb0ef41Sopenharmony_ci <div id="intro" class="interior"> 191cb0ef41Sopenharmony_ci <a href="/" title="Go back to the home page"> 201cb0ef41Sopenharmony_ci Node.js 211cb0ef41Sopenharmony_ci </a> 221cb0ef41Sopenharmony_ci </div> 231cb0ef41Sopenharmony_ci <ul> 241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 261cb0ef41Sopenharmony_ci</ul> 271cb0ef41Sopenharmony_ci<hr class="line"> 281cb0ef41Sopenharmony_ci<ul> 291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads active">Worker threads</a></li> 881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 891cb0ef41Sopenharmony_ci</ul> 901cb0ef41Sopenharmony_ci<hr class="line"> 911cb0ef41Sopenharmony_ci<ul> 921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 931cb0ef41Sopenharmony_ci</ul> 941cb0ef41Sopenharmony_ci </div> 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci <div id="column1" data-id="worker_threads" class="interior"> 971cb0ef41Sopenharmony_ci <header class="header"> 981cb0ef41Sopenharmony_ci <div class="header-container"> 991cb0ef41Sopenharmony_ci <h1>Node.js v18.20.1 documentation</h1> 1001cb0ef41Sopenharmony_ci <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 1011cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 1021cb0ef41Sopenharmony_ci <path fill="none" d="M0 0h24v24H0z" /> 1031cb0ef41Sopenharmony_ci <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"/> 1041cb0ef41Sopenharmony_ci <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"/> 1051cb0ef41Sopenharmony_ci </svg> 1061cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 1071cb0ef41Sopenharmony_ci <path d="M0 0h24v24H0z" fill="none" /> 1081cb0ef41Sopenharmony_ci <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"/> 1091cb0ef41Sopenharmony_ci </svg> 1101cb0ef41Sopenharmony_ci </button> 1111cb0ef41Sopenharmony_ci </div> 1121cb0ef41Sopenharmony_ci <div id="gtoc"> 1131cb0ef41Sopenharmony_ci <ul> 1141cb0ef41Sopenharmony_ci <li class="pinned-header">Node.js v18.20.1</li> 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci <li class="picker-header"> 1171cb0ef41Sopenharmony_ci <a href="#"> 1181cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1191cb0ef41Sopenharmony_ci Table of contents 1201cb0ef41Sopenharmony_ci </a> 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci <div class="picker"><div class="toc"><ul> 1231cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#worker-threads">Worker threads</a></span> 1241cb0ef41Sopenharmony_ci<ul> 1251cb0ef41Sopenharmony_ci<li><a href="#workergetenvironmentdatakey"><code>worker.getEnvironmentData(key)</code></a></li> 1261cb0ef41Sopenharmony_ci<li><a href="#workerismainthread"><code>worker.isMainThread</code></a></li> 1271cb0ef41Sopenharmony_ci<li><a href="#workermarkasuntransferableobject"><code>worker.markAsUntransferable(object)</code></a></li> 1281cb0ef41Sopenharmony_ci<li><a href="#workermovemessageporttocontextport-contextifiedsandbox"><code>worker.moveMessagePortToContext(port, contextifiedSandbox)</code></a></li> 1291cb0ef41Sopenharmony_ci<li><a href="#workerparentport"><code>worker.parentPort</code></a></li> 1301cb0ef41Sopenharmony_ci<li><a href="#workerreceivemessageonportport"><code>worker.receiveMessageOnPort(port)</code></a></li> 1311cb0ef41Sopenharmony_ci<li><a href="#workerresourcelimits"><code>worker.resourceLimits</code></a></li> 1321cb0ef41Sopenharmony_ci<li><a href="#workershare_env"><code>worker.SHARE_ENV</code></a></li> 1331cb0ef41Sopenharmony_ci<li><a href="#workersetenvironmentdatakey-value"><code>worker.setEnvironmentData(key[, value])</code></a></li> 1341cb0ef41Sopenharmony_ci<li><a href="#workerthreadid"><code>worker.threadId</code></a></li> 1351cb0ef41Sopenharmony_ci<li><a href="#workerworkerdata"><code>worker.workerData</code></a></li> 1361cb0ef41Sopenharmony_ci<li><a href="#class-broadcastchannel-extends-eventtarget">Class: <code>BroadcastChannel extends EventTarget</code></a> 1371cb0ef41Sopenharmony_ci<ul> 1381cb0ef41Sopenharmony_ci<li><a href="#new-broadcastchannelname"><code>new BroadcastChannel(name)</code></a></li> 1391cb0ef41Sopenharmony_ci<li><a href="#broadcastchannelclose"><code>broadcastChannel.close()</code></a></li> 1401cb0ef41Sopenharmony_ci<li><a href="#broadcastchannelonmessage"><code>broadcastChannel.onmessage</code></a></li> 1411cb0ef41Sopenharmony_ci<li><a href="#broadcastchannelonmessageerror"><code>broadcastChannel.onmessageerror</code></a></li> 1421cb0ef41Sopenharmony_ci<li><a href="#broadcastchannelpostmessagemessage"><code>broadcastChannel.postMessage(message)</code></a></li> 1431cb0ef41Sopenharmony_ci<li><a href="#broadcastchannelref"><code>broadcastChannel.ref()</code></a></li> 1441cb0ef41Sopenharmony_ci<li><a href="#broadcastchannelunref"><code>broadcastChannel.unref()</code></a></li> 1451cb0ef41Sopenharmony_ci</ul> 1461cb0ef41Sopenharmony_ci</li> 1471cb0ef41Sopenharmony_ci<li><a href="#class-messagechannel">Class: <code>MessageChannel</code></a></li> 1481cb0ef41Sopenharmony_ci<li><a href="#class-messageport">Class: <code>MessagePort</code></a> 1491cb0ef41Sopenharmony_ci<ul> 1501cb0ef41Sopenharmony_ci<li><a href="#event-close">Event: <code>'close'</code></a></li> 1511cb0ef41Sopenharmony_ci<li><a href="#event-message">Event: <code>'message'</code></a></li> 1521cb0ef41Sopenharmony_ci<li><a href="#event-messageerror">Event: <code>'messageerror'</code></a></li> 1531cb0ef41Sopenharmony_ci<li><a href="#portclose"><code>port.close()</code></a></li> 1541cb0ef41Sopenharmony_ci<li><a href="#portpostmessagevalue-transferlist"><code>port.postMessage(value[, transferList])</code></a> 1551cb0ef41Sopenharmony_ci<ul> 1561cb0ef41Sopenharmony_ci<li><a href="#considerations-when-transferring-typedarrays-and-buffers">Considerations when transferring TypedArrays and Buffers</a></li> 1571cb0ef41Sopenharmony_ci<li><a href="#considerations-when-cloning-objects-with-prototypes-classes-and-accessors">Considerations when cloning objects with prototypes, classes, and accessors</a></li> 1581cb0ef41Sopenharmony_ci</ul> 1591cb0ef41Sopenharmony_ci</li> 1601cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#porthasref"><code>port.hasRef()</code></a></span></li> 1611cb0ef41Sopenharmony_ci<li><a href="#portref"><code>port.ref()</code></a></li> 1621cb0ef41Sopenharmony_ci<li><a href="#portstart"><code>port.start()</code></a></li> 1631cb0ef41Sopenharmony_ci<li><a href="#portunref"><code>port.unref()</code></a></li> 1641cb0ef41Sopenharmony_ci</ul> 1651cb0ef41Sopenharmony_ci</li> 1661cb0ef41Sopenharmony_ci<li><a href="#class-worker">Class: <code>Worker</code></a> 1671cb0ef41Sopenharmony_ci<ul> 1681cb0ef41Sopenharmony_ci<li><a href="#new-workerfilename-options"><code>new Worker(filename[, options])</code></a></li> 1691cb0ef41Sopenharmony_ci<li><a href="#event-error">Event: <code>'error'</code></a></li> 1701cb0ef41Sopenharmony_ci<li><a href="#event-exit">Event: <code>'exit'</code></a></li> 1711cb0ef41Sopenharmony_ci<li><a href="#event-message_1">Event: <code>'message'</code></a></li> 1721cb0ef41Sopenharmony_ci<li><a href="#event-messageerror_1">Event: <code>'messageerror'</code></a></li> 1731cb0ef41Sopenharmony_ci<li><a href="#event-online">Event: <code>'online'</code></a></li> 1741cb0ef41Sopenharmony_ci<li><a href="#workergetheapsnapshot"><code>worker.getHeapSnapshot()</code></a></li> 1751cb0ef41Sopenharmony_ci<li><a href="#workerperformance"><code>worker.performance</code></a> 1761cb0ef41Sopenharmony_ci<ul> 1771cb0ef41Sopenharmony_ci<li><a href="#performanceeventlooputilizationutilization1-utilization2"><code>performance.eventLoopUtilization([utilization1[, utilization2]])</code></a></li> 1781cb0ef41Sopenharmony_ci</ul> 1791cb0ef41Sopenharmony_ci</li> 1801cb0ef41Sopenharmony_ci<li><a href="#workerpostmessagevalue-transferlist"><code>worker.postMessage(value[, transferList])</code></a></li> 1811cb0ef41Sopenharmony_ci<li><a href="#workerref"><code>worker.ref()</code></a></li> 1821cb0ef41Sopenharmony_ci<li><a href="#workerresourcelimits_1"><code>worker.resourceLimits</code></a></li> 1831cb0ef41Sopenharmony_ci<li><a href="#workerstderr"><code>worker.stderr</code></a></li> 1841cb0ef41Sopenharmony_ci<li><a href="#workerstdin"><code>worker.stdin</code></a></li> 1851cb0ef41Sopenharmony_ci<li><a href="#workerstdout"><code>worker.stdout</code></a></li> 1861cb0ef41Sopenharmony_ci<li><a href="#workerterminate"><code>worker.terminate()</code></a></li> 1871cb0ef41Sopenharmony_ci<li><a href="#workerthreadid_1"><code>worker.threadId</code></a></li> 1881cb0ef41Sopenharmony_ci<li><a href="#workerunref"><code>worker.unref()</code></a></li> 1891cb0ef41Sopenharmony_ci</ul> 1901cb0ef41Sopenharmony_ci</li> 1911cb0ef41Sopenharmony_ci<li><a href="#notes">Notes</a> 1921cb0ef41Sopenharmony_ci<ul> 1931cb0ef41Sopenharmony_ci<li><a href="#synchronous-blocking-of-stdio">Synchronous blocking of stdio</a></li> 1941cb0ef41Sopenharmony_ci<li><a href="#launching-worker-threads-from-preload-scripts">Launching worker threads from preload scripts</a></li> 1951cb0ef41Sopenharmony_ci</ul> 1961cb0ef41Sopenharmony_ci</li> 1971cb0ef41Sopenharmony_ci</ul> 1981cb0ef41Sopenharmony_ci</li> 1991cb0ef41Sopenharmony_ci</ul></div></div> 2001cb0ef41Sopenharmony_ci </li> 2011cb0ef41Sopenharmony_ci 2021cb0ef41Sopenharmony_ci 2031cb0ef41Sopenharmony_ci <li class="picker-header"> 2041cb0ef41Sopenharmony_ci <a href="#"> 2051cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2061cb0ef41Sopenharmony_ci Index 2071cb0ef41Sopenharmony_ci </a> 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ci <div class="picker"><ul> 2101cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 2111cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_ci <li> 2141cb0ef41Sopenharmony_ci <a href="index.html">Index</a> 2151cb0ef41Sopenharmony_ci </li> 2161cb0ef41Sopenharmony_ci </ul> 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci<hr class="line"> 2191cb0ef41Sopenharmony_ci<ul> 2201cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 2211cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 2221cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 2231cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 2241cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 2251cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 2261cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 2271cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 2281cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 2291cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 2301cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 2311cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 2321cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 2331cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 2341cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 2351cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 2361cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 2371cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 2381cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 2391cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 2401cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 2411cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 2421cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 2431cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 2441cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 2451cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 2461cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 2471cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 2481cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 2491cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 2501cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 2511cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 2521cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 2531cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 2541cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 2551cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 2561cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 2571cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 2581cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 2591cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 2601cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 2611cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 2621cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 2631cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 2641cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 2651cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 2661cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 2671cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 2681cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 2691cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 2701cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 2711cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 2721cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 2731cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 2741cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 2751cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 2761cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 2771cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 2781cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads active">Worker threads</a></li> 2791cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 2801cb0ef41Sopenharmony_ci</ul> 2811cb0ef41Sopenharmony_ci<hr class="line"> 2821cb0ef41Sopenharmony_ci<ul> 2831cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 2841cb0ef41Sopenharmony_ci</ul></div> 2851cb0ef41Sopenharmony_ci </li> 2861cb0ef41Sopenharmony_ci 2871cb0ef41Sopenharmony_ci 2881cb0ef41Sopenharmony_ci <li class="picker-header"> 2891cb0ef41Sopenharmony_ci <a href="#"> 2901cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2911cb0ef41Sopenharmony_ci Other versions 2921cb0ef41Sopenharmony_ci </a> 2931cb0ef41Sopenharmony_ci <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> 2941cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/worker_threads.html">20.x <b>LTS</b></a></li> 2951cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/worker_threads.html">19.x</a></li> 2961cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/worker_threads.html">18.x <b>LTS</b></a></li> 2971cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/worker_threads.html">17.x</a></li> 2981cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/worker_threads.html">16.x</a></li> 2991cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/worker_threads.html">15.x</a></li> 3001cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/worker_threads.html">14.x</a></li> 3011cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/worker_threads.html">13.x</a></li> 3021cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/worker_threads.html">12.x</a></li> 3031cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/worker_threads.html">11.x</a></li> 3041cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/worker_threads.html">10.x</a></li></ol></div> 3051cb0ef41Sopenharmony_ci </li> 3061cb0ef41Sopenharmony_ci 3071cb0ef41Sopenharmony_ci <li class="picker-header"> 3081cb0ef41Sopenharmony_ci <a href="#"> 3091cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 3101cb0ef41Sopenharmony_ci Options 3111cb0ef41Sopenharmony_ci </a> 3121cb0ef41Sopenharmony_ci 3131cb0ef41Sopenharmony_ci <div class="picker"> 3141cb0ef41Sopenharmony_ci <ul> 3151cb0ef41Sopenharmony_ci <li> 3161cb0ef41Sopenharmony_ci <a href="all.html">View on single page</a> 3171cb0ef41Sopenharmony_ci </li> 3181cb0ef41Sopenharmony_ci <li> 3191cb0ef41Sopenharmony_ci <a href="worker_threads.json">View as JSON</a> 3201cb0ef41Sopenharmony_ci </li> 3211cb0ef41Sopenharmony_ci <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/worker_threads.md">Edit on GitHub</a></li> 3221cb0ef41Sopenharmony_ci </ul> 3231cb0ef41Sopenharmony_ci </div> 3241cb0ef41Sopenharmony_ci </li> 3251cb0ef41Sopenharmony_ci </ul> 3261cb0ef41Sopenharmony_ci </div> 3271cb0ef41Sopenharmony_ci <hr> 3281cb0ef41Sopenharmony_ci </header> 3291cb0ef41Sopenharmony_ci 3301cb0ef41Sopenharmony_ci <details id="toc" open><summary>Table of contents</summary><ul> 3311cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#worker-threads">Worker threads</a></span> 3321cb0ef41Sopenharmony_ci<ul> 3331cb0ef41Sopenharmony_ci<li><a href="#workergetenvironmentdatakey"><code>worker.getEnvironmentData(key)</code></a></li> 3341cb0ef41Sopenharmony_ci<li><a href="#workerismainthread"><code>worker.isMainThread</code></a></li> 3351cb0ef41Sopenharmony_ci<li><a href="#workermarkasuntransferableobject"><code>worker.markAsUntransferable(object)</code></a></li> 3361cb0ef41Sopenharmony_ci<li><a href="#workermovemessageporttocontextport-contextifiedsandbox"><code>worker.moveMessagePortToContext(port, contextifiedSandbox)</code></a></li> 3371cb0ef41Sopenharmony_ci<li><a href="#workerparentport"><code>worker.parentPort</code></a></li> 3381cb0ef41Sopenharmony_ci<li><a href="#workerreceivemessageonportport"><code>worker.receiveMessageOnPort(port)</code></a></li> 3391cb0ef41Sopenharmony_ci<li><a href="#workerresourcelimits"><code>worker.resourceLimits</code></a></li> 3401cb0ef41Sopenharmony_ci<li><a href="#workershare_env"><code>worker.SHARE_ENV</code></a></li> 3411cb0ef41Sopenharmony_ci<li><a href="#workersetenvironmentdatakey-value"><code>worker.setEnvironmentData(key[, value])</code></a></li> 3421cb0ef41Sopenharmony_ci<li><a href="#workerthreadid"><code>worker.threadId</code></a></li> 3431cb0ef41Sopenharmony_ci<li><a href="#workerworkerdata"><code>worker.workerData</code></a></li> 3441cb0ef41Sopenharmony_ci<li><a href="#class-broadcastchannel-extends-eventtarget">Class: <code>BroadcastChannel extends EventTarget</code></a> 3451cb0ef41Sopenharmony_ci<ul> 3461cb0ef41Sopenharmony_ci<li><a href="#new-broadcastchannelname"><code>new BroadcastChannel(name)</code></a></li> 3471cb0ef41Sopenharmony_ci<li><a href="#broadcastchannelclose"><code>broadcastChannel.close()</code></a></li> 3481cb0ef41Sopenharmony_ci<li><a href="#broadcastchannelonmessage"><code>broadcastChannel.onmessage</code></a></li> 3491cb0ef41Sopenharmony_ci<li><a href="#broadcastchannelonmessageerror"><code>broadcastChannel.onmessageerror</code></a></li> 3501cb0ef41Sopenharmony_ci<li><a href="#broadcastchannelpostmessagemessage"><code>broadcastChannel.postMessage(message)</code></a></li> 3511cb0ef41Sopenharmony_ci<li><a href="#broadcastchannelref"><code>broadcastChannel.ref()</code></a></li> 3521cb0ef41Sopenharmony_ci<li><a href="#broadcastchannelunref"><code>broadcastChannel.unref()</code></a></li> 3531cb0ef41Sopenharmony_ci</ul> 3541cb0ef41Sopenharmony_ci</li> 3551cb0ef41Sopenharmony_ci<li><a href="#class-messagechannel">Class: <code>MessageChannel</code></a></li> 3561cb0ef41Sopenharmony_ci<li><a href="#class-messageport">Class: <code>MessagePort</code></a> 3571cb0ef41Sopenharmony_ci<ul> 3581cb0ef41Sopenharmony_ci<li><a href="#event-close">Event: <code>'close'</code></a></li> 3591cb0ef41Sopenharmony_ci<li><a href="#event-message">Event: <code>'message'</code></a></li> 3601cb0ef41Sopenharmony_ci<li><a href="#event-messageerror">Event: <code>'messageerror'</code></a></li> 3611cb0ef41Sopenharmony_ci<li><a href="#portclose"><code>port.close()</code></a></li> 3621cb0ef41Sopenharmony_ci<li><a href="#portpostmessagevalue-transferlist"><code>port.postMessage(value[, transferList])</code></a> 3631cb0ef41Sopenharmony_ci<ul> 3641cb0ef41Sopenharmony_ci<li><a href="#considerations-when-transferring-typedarrays-and-buffers">Considerations when transferring TypedArrays and Buffers</a></li> 3651cb0ef41Sopenharmony_ci<li><a href="#considerations-when-cloning-objects-with-prototypes-classes-and-accessors">Considerations when cloning objects with prototypes, classes, and accessors</a></li> 3661cb0ef41Sopenharmony_ci</ul> 3671cb0ef41Sopenharmony_ci</li> 3681cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#porthasref"><code>port.hasRef()</code></a></span></li> 3691cb0ef41Sopenharmony_ci<li><a href="#portref"><code>port.ref()</code></a></li> 3701cb0ef41Sopenharmony_ci<li><a href="#portstart"><code>port.start()</code></a></li> 3711cb0ef41Sopenharmony_ci<li><a href="#portunref"><code>port.unref()</code></a></li> 3721cb0ef41Sopenharmony_ci</ul> 3731cb0ef41Sopenharmony_ci</li> 3741cb0ef41Sopenharmony_ci<li><a href="#class-worker">Class: <code>Worker</code></a> 3751cb0ef41Sopenharmony_ci<ul> 3761cb0ef41Sopenharmony_ci<li><a href="#new-workerfilename-options"><code>new Worker(filename[, options])</code></a></li> 3771cb0ef41Sopenharmony_ci<li><a href="#event-error">Event: <code>'error'</code></a></li> 3781cb0ef41Sopenharmony_ci<li><a href="#event-exit">Event: <code>'exit'</code></a></li> 3791cb0ef41Sopenharmony_ci<li><a href="#event-message_1">Event: <code>'message'</code></a></li> 3801cb0ef41Sopenharmony_ci<li><a href="#event-messageerror_1">Event: <code>'messageerror'</code></a></li> 3811cb0ef41Sopenharmony_ci<li><a href="#event-online">Event: <code>'online'</code></a></li> 3821cb0ef41Sopenharmony_ci<li><a href="#workergetheapsnapshot"><code>worker.getHeapSnapshot()</code></a></li> 3831cb0ef41Sopenharmony_ci<li><a href="#workerperformance"><code>worker.performance</code></a> 3841cb0ef41Sopenharmony_ci<ul> 3851cb0ef41Sopenharmony_ci<li><a href="#performanceeventlooputilizationutilization1-utilization2"><code>performance.eventLoopUtilization([utilization1[, utilization2]])</code></a></li> 3861cb0ef41Sopenharmony_ci</ul> 3871cb0ef41Sopenharmony_ci</li> 3881cb0ef41Sopenharmony_ci<li><a href="#workerpostmessagevalue-transferlist"><code>worker.postMessage(value[, transferList])</code></a></li> 3891cb0ef41Sopenharmony_ci<li><a href="#workerref"><code>worker.ref()</code></a></li> 3901cb0ef41Sopenharmony_ci<li><a href="#workerresourcelimits_1"><code>worker.resourceLimits</code></a></li> 3911cb0ef41Sopenharmony_ci<li><a href="#workerstderr"><code>worker.stderr</code></a></li> 3921cb0ef41Sopenharmony_ci<li><a href="#workerstdin"><code>worker.stdin</code></a></li> 3931cb0ef41Sopenharmony_ci<li><a href="#workerstdout"><code>worker.stdout</code></a></li> 3941cb0ef41Sopenharmony_ci<li><a href="#workerterminate"><code>worker.terminate()</code></a></li> 3951cb0ef41Sopenharmony_ci<li><a href="#workerthreadid_1"><code>worker.threadId</code></a></li> 3961cb0ef41Sopenharmony_ci<li><a href="#workerunref"><code>worker.unref()</code></a></li> 3971cb0ef41Sopenharmony_ci</ul> 3981cb0ef41Sopenharmony_ci</li> 3991cb0ef41Sopenharmony_ci<li><a href="#notes">Notes</a> 4001cb0ef41Sopenharmony_ci<ul> 4011cb0ef41Sopenharmony_ci<li><a href="#synchronous-blocking-of-stdio">Synchronous blocking of stdio</a></li> 4021cb0ef41Sopenharmony_ci<li><a href="#launching-worker-threads-from-preload-scripts">Launching worker threads from preload scripts</a></li> 4031cb0ef41Sopenharmony_ci</ul> 4041cb0ef41Sopenharmony_ci</li> 4051cb0ef41Sopenharmony_ci</ul> 4061cb0ef41Sopenharmony_ci</li> 4071cb0ef41Sopenharmony_ci</ul></details> 4081cb0ef41Sopenharmony_ci 4091cb0ef41Sopenharmony_ci <div id="apicontent"> 4101cb0ef41Sopenharmony_ci <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> 4111cb0ef41Sopenharmony_ci 4121cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 4131cb0ef41Sopenharmony_ci<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> 4141cb0ef41Sopenharmony_ci<p>The <code>node:worker_threads</code> module enables the use of threads that execute 4151cb0ef41Sopenharmony_ciJavaScript in parallel. To access it:</p> 4161cb0ef41Sopenharmony_ci<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> 4171cb0ef41Sopenharmony_ci<p>Workers (threads) are useful for performing CPU-intensive JavaScript operations. 4181cb0ef41Sopenharmony_ciThey do not help much with I/O-intensive work. The Node.js built-in 4191cb0ef41Sopenharmony_ciasynchronous I/O operations are more efficient than Workers can be.</p> 4201cb0ef41Sopenharmony_ci<p>Unlike <code>child_process</code> or <code>cluster</code>, <code>worker_threads</code> can share memory. They do 4211cb0ef41Sopenharmony_ciso by transferring <code>ArrayBuffer</code> instances or sharing <code>SharedArrayBuffer</code> 4221cb0ef41Sopenharmony_ciinstances.</p> 4231cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> { 4241cb0ef41Sopenharmony_ci <span class="hljs-title class_">Worker</span>, isMainThread, parentPort, workerData, 4251cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>); 4261cb0ef41Sopenharmony_ci 4271cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (isMainThread) { 4281cb0ef41Sopenharmony_ci <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>) { 4291cb0ef41Sopenharmony_ci <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> { 4301cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename, { 4311cb0ef41Sopenharmony_ci <span class="hljs-attr">workerData</span>: script, 4321cb0ef41Sopenharmony_ci }); 4331cb0ef41Sopenharmony_ci worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, resolve); 4341cb0ef41Sopenharmony_ci worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, reject); 4351cb0ef41Sopenharmony_ci 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> { 4361cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>) 4371cb0ef41Sopenharmony_ci <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>)); 4381cb0ef41Sopenharmony_ci }); 4391cb0ef41Sopenharmony_ci }); 4401cb0ef41Sopenharmony_ci }; 4411cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> { 4421cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> { parse } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'some-js-parsing-library'</span>); 4431cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> script = workerData; 4441cb0ef41Sopenharmony_ci parentPort.<span class="hljs-title function_">postMessage</span>(<span class="hljs-title function_">parse</span>(script)); 4451cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 4461cb0ef41Sopenharmony_ci<p>The above example spawns a Worker thread for each <code>parseJSAsync()</code> call. In 4471cb0ef41Sopenharmony_cipractice, use a pool of Workers for these kinds of tasks. Otherwise, the 4481cb0ef41Sopenharmony_cioverhead of creating Workers would likely exceed their benefit.</p> 4491cb0ef41Sopenharmony_ci<p>When implementing a worker pool, use the <a href="async_hooks.html#class-asyncresource"><code>AsyncResource</code></a> API to inform 4501cb0ef41Sopenharmony_cidiagnostic tools (e.g. to provide asynchronous stack traces) about the 4511cb0ef41Sopenharmony_cicorrelation between tasks and their outcomes. See 4521cb0ef41Sopenharmony_ci<a href="async_context.html#using-asyncresource-for-a-worker-thread-pool">"Using <code>AsyncResource</code> for a <code>Worker</code> thread pool"</a> 4531cb0ef41Sopenharmony_ciin the <code>async_hooks</code> documentation for an example implementation.</p> 4541cb0ef41Sopenharmony_ci<p>Worker threads inherit non-process-specific options by default. Refer to 4551cb0ef41Sopenharmony_ci<a href="#new-workerfilename-options"><code>Worker constructor options</code></a> to know how to customize worker thread options, 4561cb0ef41Sopenharmony_cispecifically <code>argv</code> and <code>execArgv</code> options.</p> 4571cb0ef41Sopenharmony_ci<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> 4581cb0ef41Sopenharmony_ci<div class="api_metadata"> 4591cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 4601cb0ef41Sopenharmony_ci<table> 4611cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 4621cb0ef41Sopenharmony_ci<tr><td>v17.5.0</td> 4631cb0ef41Sopenharmony_ci<td><p>No longer experimental.</p></td></tr> 4641cb0ef41Sopenharmony_ci<tr><td>v15.12.0, v14.18.0</td> 4651cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.12.0, v14.18.0</span></p></td></tr> 4661cb0ef41Sopenharmony_ci</tbody></table> 4671cb0ef41Sopenharmony_ci</details> 4681cb0ef41Sopenharmony_ci</div> 4691cb0ef41Sopenharmony_ci<ul> 4701cb0ef41Sopenharmony_ci<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Any arbitrary, cloneable JavaScript value that can be used as a 4711cb0ef41Sopenharmony_ci<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" class="type"><Map></a> key.</li> 4721cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 4731cb0ef41Sopenharmony_ci</ul> 4741cb0ef41Sopenharmony_ci<p>Within a worker thread, <code>worker.getEnvironmentData()</code> returns a clone 4751cb0ef41Sopenharmony_ciof data passed to the spawning thread's <code>worker.setEnvironmentData()</code>. 4761cb0ef41Sopenharmony_ciEvery new <code>Worker</code> receives its own copy of the environment data 4771cb0ef41Sopenharmony_ciautomatically.</p> 4781cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> { 4791cb0ef41Sopenharmony_ci <span class="hljs-title class_">Worker</span>, 4801cb0ef41Sopenharmony_ci isMainThread, 4811cb0ef41Sopenharmony_ci setEnvironmentData, 4821cb0ef41Sopenharmony_ci getEnvironmentData, 4831cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>); 4841cb0ef41Sopenharmony_ci 4851cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (isMainThread) { 4861cb0ef41Sopenharmony_ci <span class="hljs-title function_">setEnvironmentData</span>(<span class="hljs-string">'Hello'</span>, <span class="hljs-string">'World!'</span>); 4871cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename); 4881cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> { 4891cb0ef41Sopenharmony_ci <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> 4901cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 4911cb0ef41Sopenharmony_ci</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> 4921cb0ef41Sopenharmony_ci<div class="api_metadata"> 4931cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 4941cb0ef41Sopenharmony_ci</div> 4951cb0ef41Sopenharmony_ci<ul> 4961cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 4971cb0ef41Sopenharmony_ci</ul> 4981cb0ef41Sopenharmony_ci<p>Is <code>true</code> if this code is not running inside of a <a href="#class-worker"><code>Worker</code></a> thread.</p> 4991cb0ef41Sopenharmony_ci<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>); 5001cb0ef41Sopenharmony_ci 5011cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (isMainThread) { 5021cb0ef41Sopenharmony_ci <span class="hljs-comment">// This re-loads the current file inside a Worker instance.</span> 5031cb0ef41Sopenharmony_ci <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename); 5041cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> { 5051cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Inside Worker!'</span>); 5061cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(isMainThread); <span class="hljs-comment">// Prints 'false'.</span> 5071cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 5081cb0ef41Sopenharmony_ci</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> 5091cb0ef41Sopenharmony_ci<div class="api_metadata"> 5101cb0ef41Sopenharmony_ci<span>Added in: v14.5.0, v12.19.0</span> 5111cb0ef41Sopenharmony_ci</div> 5121cb0ef41Sopenharmony_ci<p>Mark an object as not transferable. If <code>object</code> occurs in the transfer list of 5131cb0ef41Sopenharmony_cia <a href="#portpostmessagevalue-transferlist"><code>port.postMessage()</code></a> call, it is ignored.</p> 5141cb0ef41Sopenharmony_ci<p>In particular, this makes sense for objects that can be cloned, rather than 5151cb0ef41Sopenharmony_citransferred, and which are used by other objects on the sending side. 5161cb0ef41Sopenharmony_ciFor example, Node.js marks the <code>ArrayBuffer</code>s it uses for its 5171cb0ef41Sopenharmony_ci<a href="buffer.html#static-method-bufferallocunsafesize"><code>Buffer</code> pool</a> with this.</p> 5181cb0ef41Sopenharmony_ci<p>This operation cannot be undone.</p> 5191cb0ef41Sopenharmony_ci<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>); 5201cb0ef41Sopenharmony_ci 5211cb0ef41Sopenharmony_ci<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>); 5221cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> typedArray1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(pooledBuffer); 5231cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> typedArray2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Float64Array</span>(pooledBuffer); 5241cb0ef41Sopenharmony_ci 5251cb0ef41Sopenharmony_ci<span class="hljs-title function_">markAsUntransferable</span>(pooledBuffer); 5261cb0ef41Sopenharmony_ci 5271cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { port1 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 5281cb0ef41Sopenharmony_ciport1.<span class="hljs-title function_">postMessage</span>(typedArray1, [ typedArray1.<span class="hljs-property">buffer</span> ]); 5291cb0ef41Sopenharmony_ci 5301cb0ef41Sopenharmony_ci<span class="hljs-comment">// The following line prints the contents of typedArray1 -- it still owns</span> 5311cb0ef41Sopenharmony_ci<span class="hljs-comment">// its memory and has been cloned, not transferred. Without</span> 5321cb0ef41Sopenharmony_ci<span class="hljs-comment">// `markAsUntransferable()`, this would print an empty Uint8Array.</span> 5331cb0ef41Sopenharmony_ci<span class="hljs-comment">// typedArray2 is intact as well.</span> 5341cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(typedArray1); 5351cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(typedArray2);</code> <button class="copy-button">copy</button></pre> 5361cb0ef41Sopenharmony_ci<p>There is no equivalent to this API in browsers.</p> 5371cb0ef41Sopenharmony_ci</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> 5381cb0ef41Sopenharmony_ci<div class="api_metadata"> 5391cb0ef41Sopenharmony_ci<span>Added in: v11.13.0</span> 5401cb0ef41Sopenharmony_ci</div> 5411cb0ef41Sopenharmony_ci<ul> 5421cb0ef41Sopenharmony_ci<li> 5431cb0ef41Sopenharmony_ci<p><code>port</code> <a href="worker_threads.html#class-messageport" class="type"><MessagePort></a> The message port to transfer.</p> 5441cb0ef41Sopenharmony_ci</li> 5451cb0ef41Sopenharmony_ci<li> 5461cb0ef41Sopenharmony_ci<p><code>contextifiedSandbox</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> A <a href="vm.html#what-does-it-mean-to-contextify-an-object">contextified</a> object as returned by the 5471cb0ef41Sopenharmony_ci<code>vm.createContext()</code> method.</p> 5481cb0ef41Sopenharmony_ci</li> 5491cb0ef41Sopenharmony_ci<li> 5501cb0ef41Sopenharmony_ci<p>Returns: <a href="worker_threads.html#class-messageport" class="type"><MessagePort></a></p> 5511cb0ef41Sopenharmony_ci</li> 5521cb0ef41Sopenharmony_ci</ul> 5531cb0ef41Sopenharmony_ci<p>Transfer a <code>MessagePort</code> to a different <a href="vm.html"><code>vm</code></a> Context. The original <code>port</code> 5541cb0ef41Sopenharmony_ciobject is rendered unusable, and the returned <code>MessagePort</code> instance 5551cb0ef41Sopenharmony_citakes its place.</p> 5561cb0ef41Sopenharmony_ci<p>The returned <code>MessagePort</code> is an object in the target context and 5571cb0ef41Sopenharmony_ciinherits from its global <code>Object</code> class. Objects passed to the 5581cb0ef41Sopenharmony_ci<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 5591cb0ef41Sopenharmony_ciand inherit from its global <code>Object</code> class.</p> 5601cb0ef41Sopenharmony_ci<p>However, the created <code>MessagePort</code> no longer inherits from 5611cb0ef41Sopenharmony_ci<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 5621cb0ef41Sopenharmony_cievents using it.</p> 5631cb0ef41Sopenharmony_ci</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> 5641cb0ef41Sopenharmony_ci<div class="api_metadata"> 5651cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 5661cb0ef41Sopenharmony_ci</div> 5671cb0ef41Sopenharmony_ci<ul> 5681cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> | <a href="worker_threads.html#class-messageport" class="type"><MessagePort></a></li> 5691cb0ef41Sopenharmony_ci</ul> 5701cb0ef41Sopenharmony_ci<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> 5711cb0ef41Sopenharmony_ciallowing communication with the parent thread. Messages sent using 5721cb0ef41Sopenharmony_ci<code>parentPort.postMessage()</code> are available in the parent thread 5731cb0ef41Sopenharmony_ciusing <code>worker.on('message')</code>, and messages sent from the parent thread 5741cb0ef41Sopenharmony_ciusing <code>worker.postMessage()</code> are available in this thread using 5751cb0ef41Sopenharmony_ci<code>parentPort.on('message')</code>.</p> 5761cb0ef41Sopenharmony_ci<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>); 5771cb0ef41Sopenharmony_ci 5781cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (isMainThread) { 5791cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename); 5801cb0ef41Sopenharmony_ci 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> { 5811cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(message); <span class="hljs-comment">// Prints 'Hello, world!'.</span> 5821cb0ef41Sopenharmony_ci }); 5831cb0ef41Sopenharmony_ci worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'Hello, world!'</span>); 5841cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> { 5851cb0ef41Sopenharmony_ci <span class="hljs-comment">// When a message from the parent thread is received, send it back:</span> 5861cb0ef41Sopenharmony_ci 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> { 5871cb0ef41Sopenharmony_ci parentPort.<span class="hljs-title function_">postMessage</span>(message); 5881cb0ef41Sopenharmony_ci }); 5891cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 5901cb0ef41Sopenharmony_ci</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> 5911cb0ef41Sopenharmony_ci<div class="api_metadata"> 5921cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 5931cb0ef41Sopenharmony_ci<table> 5941cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 5951cb0ef41Sopenharmony_ci<tr><td>v15.12.0</td> 5961cb0ef41Sopenharmony_ci<td><p>The port argument can also refer to a <code>BroadcastChannel</code> now.</p></td></tr> 5971cb0ef41Sopenharmony_ci<tr><td>v12.3.0</td> 5981cb0ef41Sopenharmony_ci<td><p><span>Added in: v12.3.0</span></p></td></tr> 5991cb0ef41Sopenharmony_ci</tbody></table> 6001cb0ef41Sopenharmony_ci</details> 6011cb0ef41Sopenharmony_ci</div> 6021cb0ef41Sopenharmony_ci<ul> 6031cb0ef41Sopenharmony_ci<li> 6041cb0ef41Sopenharmony_ci<p><code>port</code> <a href="worker_threads.html#class-messageport" class="type"><MessagePort></a> | <a href="worker_threads.html#class-broadcastchannel-extends-eventtarget" class="type"><BroadcastChannel></a></p> 6051cb0ef41Sopenharmony_ci</li> 6061cb0ef41Sopenharmony_ci<li> 6071cb0ef41Sopenharmony_ci<p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></p> 6081cb0ef41Sopenharmony_ci</li> 6091cb0ef41Sopenharmony_ci</ul> 6101cb0ef41Sopenharmony_ci<p>Receive a single message from a given <code>MessagePort</code>. If no message is available, 6111cb0ef41Sopenharmony_ci<code>undefined</code> is returned, otherwise an object with a single <code>message</code> property 6121cb0ef41Sopenharmony_cithat contains the message payload, corresponding to the oldest message in the 6131cb0ef41Sopenharmony_ci<code>MessagePort</code>'s queue.</p> 6141cb0ef41Sopenharmony_ci<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>); 6151cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 6161cb0ef41Sopenharmony_ciport1.<span class="hljs-title function_">postMessage</span>({ <span class="hljs-attr">hello</span>: <span class="hljs-string">'world'</span> }); 6171cb0ef41Sopenharmony_ci 6181cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">receiveMessageOnPort</span>(port2)); 6191cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: { message: { hello: 'world' } }</span> 6201cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">receiveMessageOnPort</span>(port2)); 6211cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: undefined</span></code> <button class="copy-button">copy</button></pre> 6221cb0ef41Sopenharmony_ci<p>When this function is used, no <code>'message'</code> event is emitted and the 6231cb0ef41Sopenharmony_ci<code>onmessage</code> listener is not invoked.</p> 6241cb0ef41Sopenharmony_ci</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> 6251cb0ef41Sopenharmony_ci<div class="api_metadata"> 6261cb0ef41Sopenharmony_ci<span>Added in: v13.2.0, v12.16.0</span> 6271cb0ef41Sopenharmony_ci</div> 6281cb0ef41Sopenharmony_ci<ul> 6291cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6301cb0ef41Sopenharmony_ci<ul> 6311cb0ef41Sopenharmony_ci<li><code>maxYoungGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 6321cb0ef41Sopenharmony_ci<li><code>maxOldGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 6331cb0ef41Sopenharmony_ci<li><code>codeRangeSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 6341cb0ef41Sopenharmony_ci<li><code>stackSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 6351cb0ef41Sopenharmony_ci</ul> 6361cb0ef41Sopenharmony_ci</li> 6371cb0ef41Sopenharmony_ci</ul> 6381cb0ef41Sopenharmony_ci<p>Provides the set of JS engine resource constraints inside this Worker thread. 6391cb0ef41Sopenharmony_ciIf the <code>resourceLimits</code> option was passed to the <a href="#class-worker"><code>Worker</code></a> constructor, 6401cb0ef41Sopenharmony_cithis matches its values.</p> 6411cb0ef41Sopenharmony_ci<p>If this is used in the main thread, its value is an empty object.</p> 6421cb0ef41Sopenharmony_ci</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> 6431cb0ef41Sopenharmony_ci<div class="api_metadata"> 6441cb0ef41Sopenharmony_ci<span>Added in: v11.14.0</span> 6451cb0ef41Sopenharmony_ci</div> 6461cb0ef41Sopenharmony_ci<ul> 6471cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a></li> 6481cb0ef41Sopenharmony_ci</ul> 6491cb0ef41Sopenharmony_ci<p>A special value that can be passed as the <code>env</code> option of the <a href="#class-worker"><code>Worker</code></a> 6501cb0ef41Sopenharmony_ciconstructor, to indicate that the current thread and the Worker thread should 6511cb0ef41Sopenharmony_cishare read and write access to the same set of environment variables.</p> 6521cb0ef41Sopenharmony_ci<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>); 6531cb0ef41Sopenharmony_ci<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> }) 6541cb0ef41Sopenharmony_ci .<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> { 6551cb0ef41Sopenharmony_ci <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> 6561cb0ef41Sopenharmony_ci });</code> <button class="copy-button">copy</button></pre> 6571cb0ef41Sopenharmony_ci</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> 6581cb0ef41Sopenharmony_ci<div class="api_metadata"> 6591cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 6601cb0ef41Sopenharmony_ci<table> 6611cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 6621cb0ef41Sopenharmony_ci<tr><td>v17.5.0</td> 6631cb0ef41Sopenharmony_ci<td><p>No longer experimental.</p></td></tr> 6641cb0ef41Sopenharmony_ci<tr><td>v15.12.0, v14.18.0</td> 6651cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.12.0, v14.18.0</span></p></td></tr> 6661cb0ef41Sopenharmony_ci</tbody></table> 6671cb0ef41Sopenharmony_ci</details> 6681cb0ef41Sopenharmony_ci</div> 6691cb0ef41Sopenharmony_ci<ul> 6701cb0ef41Sopenharmony_ci<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Any arbitrary, cloneable JavaScript value that can be used as a 6711cb0ef41Sopenharmony_ci<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" class="type"><Map></a> key.</li> 6721cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Any arbitrary, cloneable JavaScript value that will be cloned 6731cb0ef41Sopenharmony_ciand passed automatically to all new <code>Worker</code> instances. If <code>value</code> is passed 6741cb0ef41Sopenharmony_cias <code>undefined</code>, any previously set value for the <code>key</code> will be deleted.</li> 6751cb0ef41Sopenharmony_ci</ul> 6761cb0ef41Sopenharmony_ci<p>The <code>worker.setEnvironmentData()</code> API sets the content of 6771cb0ef41Sopenharmony_ci<code>worker.getEnvironmentData()</code> in the current thread and all new <code>Worker</code> 6781cb0ef41Sopenharmony_ciinstances spawned from the current context.</p> 6791cb0ef41Sopenharmony_ci</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> 6801cb0ef41Sopenharmony_ci<div class="api_metadata"> 6811cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 6821cb0ef41Sopenharmony_ci</div> 6831cb0ef41Sopenharmony_ci<ul> 6841cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6851cb0ef41Sopenharmony_ci</ul> 6861cb0ef41Sopenharmony_ci<p>An integer identifier for the current thread. On the corresponding worker object 6871cb0ef41Sopenharmony_ci(if there is any), it is available as <a href="#workerthreadid_1"><code>worker.threadId</code></a>. 6881cb0ef41Sopenharmony_ciThis value is unique for each <a href="#class-worker"><code>Worker</code></a> instance inside a single process.</p> 6891cb0ef41Sopenharmony_ci</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> 6901cb0ef41Sopenharmony_ci<div class="api_metadata"> 6911cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 6921cb0ef41Sopenharmony_ci</div> 6931cb0ef41Sopenharmony_ci<p>An arbitrary JavaScript value that contains a clone of the data passed 6941cb0ef41Sopenharmony_cito this thread's <code>Worker</code> constructor.</p> 6951cb0ef41Sopenharmony_ci<p>The data is cloned as if using <a href="#portpostmessagevalue-transferlist"><code>postMessage()</code></a>, 6961cb0ef41Sopenharmony_ciaccording 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> 6971cb0ef41Sopenharmony_ci<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>); 6981cb0ef41Sopenharmony_ci 6991cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (isMainThread) { 7001cb0ef41Sopenharmony_ci <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> }); 7011cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> { 7021cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(workerData); <span class="hljs-comment">// Prints 'Hello, world!'.</span> 7031cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 7041cb0ef41Sopenharmony_ci</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> 7051cb0ef41Sopenharmony_ci<div class="api_metadata"> 7061cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 7071cb0ef41Sopenharmony_ci<table> 7081cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 7091cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 7101cb0ef41Sopenharmony_ci<td><p>No longer experimental.</p></td></tr> 7111cb0ef41Sopenharmony_ci<tr><td>v15.4.0</td> 7121cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.4.0</span></p></td></tr> 7131cb0ef41Sopenharmony_ci</tbody></table> 7141cb0ef41Sopenharmony_ci</details> 7151cb0ef41Sopenharmony_ci</div> 7161cb0ef41Sopenharmony_ci<p>Instances of <code>BroadcastChannel</code> allow asynchronous one-to-many communication 7171cb0ef41Sopenharmony_ciwith all other <code>BroadcastChannel</code> instances bound to the same channel name.</p> 7181cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-meta">'use strict'</span>; 7191cb0ef41Sopenharmony_ci 7201cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 7211cb0ef41Sopenharmony_ci isMainThread, 7221cb0ef41Sopenharmony_ci <span class="hljs-title class_">BroadcastChannel</span>, 7231cb0ef41Sopenharmony_ci <span class="hljs-title class_">Worker</span>, 7241cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>); 7251cb0ef41Sopenharmony_ci 7261cb0ef41Sopenharmony_ci<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>); 7271cb0ef41Sopenharmony_ci 7281cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (isMainThread) { 7291cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> c = <span class="hljs-number">0</span>; 7301cb0ef41Sopenharmony_ci bc.<span class="hljs-property">onmessage</span> = <span class="hljs-function">(<span class="hljs-params">event</span>) =></span> { 7311cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span class="hljs-property">data</span>); 7321cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (++c === <span class="hljs-number">10</span>) bc.<span class="hljs-title function_">close</span>(); 7331cb0ef41Sopenharmony_ci }; 7341cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-number">0</span>; n < <span class="hljs-number">10</span>; n++) 7351cb0ef41Sopenharmony_ci <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename); 7361cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> { 7371cb0ef41Sopenharmony_ci bc.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'hello from every worker'</span>); 7381cb0ef41Sopenharmony_ci bc.<span class="hljs-title function_">close</span>(); 7391cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 7401cb0ef41Sopenharmony_ci<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> 7411cb0ef41Sopenharmony_ci<div class="api_metadata"> 7421cb0ef41Sopenharmony_ci<span>Added in: v15.4.0</span> 7431cb0ef41Sopenharmony_ci</div> 7441cb0ef41Sopenharmony_ci<ul> 7451cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The name of the channel to connect to. Any JavaScript value 7461cb0ef41Sopenharmony_cithat can be converted to a string using <code>`${name}`</code> is permitted.</li> 7471cb0ef41Sopenharmony_ci</ul> 7481cb0ef41Sopenharmony_ci<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> 7491cb0ef41Sopenharmony_ci<div class="api_metadata"> 7501cb0ef41Sopenharmony_ci<span>Added in: v15.4.0</span> 7511cb0ef41Sopenharmony_ci</div> 7521cb0ef41Sopenharmony_ci<p>Closes the <code>BroadcastChannel</code> connection.</p> 7531cb0ef41Sopenharmony_ci<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> 7541cb0ef41Sopenharmony_ci<div class="api_metadata"> 7551cb0ef41Sopenharmony_ci<span>Added in: v15.4.0</span> 7561cb0ef41Sopenharmony_ci</div> 7571cb0ef41Sopenharmony_ci<ul> 7581cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked with a single <code>MessageEvent</code> argument 7591cb0ef41Sopenharmony_ciwhen a message is received.</li> 7601cb0ef41Sopenharmony_ci</ul> 7611cb0ef41Sopenharmony_ci<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> 7621cb0ef41Sopenharmony_ci<div class="api_metadata"> 7631cb0ef41Sopenharmony_ci<span>Added in: v15.4.0</span> 7641cb0ef41Sopenharmony_ci</div> 7651cb0ef41Sopenharmony_ci<ul> 7661cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked with a received message cannot be 7671cb0ef41Sopenharmony_cideserialized.</li> 7681cb0ef41Sopenharmony_ci</ul> 7691cb0ef41Sopenharmony_ci<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> 7701cb0ef41Sopenharmony_ci<div class="api_metadata"> 7711cb0ef41Sopenharmony_ci<span>Added in: v15.4.0</span> 7721cb0ef41Sopenharmony_ci</div> 7731cb0ef41Sopenharmony_ci<ul> 7741cb0ef41Sopenharmony_ci<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Any cloneable JavaScript value.</li> 7751cb0ef41Sopenharmony_ci</ul> 7761cb0ef41Sopenharmony_ci<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> 7771cb0ef41Sopenharmony_ci<div class="api_metadata"> 7781cb0ef41Sopenharmony_ci<span>Added in: v15.4.0</span> 7791cb0ef41Sopenharmony_ci</div> 7801cb0ef41Sopenharmony_ci<p>Opposite of <code>unref()</code>. Calling <code>ref()</code> on a previously <code>unref()</code>ed 7811cb0ef41Sopenharmony_ciBroadcastChannel does <em>not</em> let the program exit if it's the only active handle 7821cb0ef41Sopenharmony_cileft (the default behavior). If the port is <code>ref()</code>ed, calling <code>ref()</code> again 7831cb0ef41Sopenharmony_cihas no effect.</p> 7841cb0ef41Sopenharmony_ci<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> 7851cb0ef41Sopenharmony_ci<div class="api_metadata"> 7861cb0ef41Sopenharmony_ci<span>Added in: v15.4.0</span> 7871cb0ef41Sopenharmony_ci</div> 7881cb0ef41Sopenharmony_ci<p>Calling <code>unref()</code> on a BroadcastChannel allows the thread to exit if this 7891cb0ef41Sopenharmony_ciis the only active handle in the event system. If the BroadcastChannel is 7901cb0ef41Sopenharmony_cialready <code>unref()</code>ed calling <code>unref()</code> again has no effect.</p> 7911cb0ef41Sopenharmony_ci</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> 7921cb0ef41Sopenharmony_ci<div class="api_metadata"> 7931cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 7941cb0ef41Sopenharmony_ci</div> 7951cb0ef41Sopenharmony_ci<p>Instances of the <code>worker.MessageChannel</code> class represent an asynchronous, 7961cb0ef41Sopenharmony_citwo-way communications channel. 7971cb0ef41Sopenharmony_ciThe <code>MessageChannel</code> has no methods of its own. <code>new MessageChannel()</code> 7981cb0ef41Sopenharmony_ciyields an object with <code>port1</code> and <code>port2</code> properties, which refer to linked 7991cb0ef41Sopenharmony_ci<a href="#class-messageport"><code>MessagePort</code></a> instances.</p> 8001cb0ef41Sopenharmony_ci<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>); 8011cb0ef41Sopenharmony_ci 8021cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 8031cb0ef41Sopenharmony_ciport1.<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)); 8041cb0ef41Sopenharmony_ciport2.<span class="hljs-title function_">postMessage</span>({ <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span> }); 8051cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: received { foo: 'bar' } from the `port1.on('message')` listener</span></code> <button class="copy-button">copy</button></pre> 8061cb0ef41Sopenharmony_ci</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> 8071cb0ef41Sopenharmony_ci<div class="api_metadata"> 8081cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 8091cb0ef41Sopenharmony_ci<table> 8101cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 8111cb0ef41Sopenharmony_ci<tr><td>v14.7.0</td> 8121cb0ef41Sopenharmony_ci<td><p>This class now inherits from <code>EventTarget</code> rather than from <code>EventEmitter</code>.</p></td></tr> 8131cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td> 8141cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.5.0</span></p></td></tr> 8151cb0ef41Sopenharmony_ci</tbody></table> 8161cb0ef41Sopenharmony_ci</details> 8171cb0ef41Sopenharmony_ci</div> 8181cb0ef41Sopenharmony_ci<ul> 8191cb0ef41Sopenharmony_ci<li>Extends: <a href="events.html#class-eventtarget" class="type"><EventTarget></a></li> 8201cb0ef41Sopenharmony_ci</ul> 8211cb0ef41Sopenharmony_ci<p>Instances of the <code>worker.MessagePort</code> class represent one end of an 8221cb0ef41Sopenharmony_ciasynchronous, two-way communications channel. It can be used to transfer 8231cb0ef41Sopenharmony_cistructured data, memory regions and other <code>MessagePort</code>s between different 8241cb0ef41Sopenharmony_ci<a href="#class-worker"><code>Worker</code></a>s.</p> 8251cb0ef41Sopenharmony_ci<p>This implementation matches <a href="https://developer.mozilla.org/en-US/docs/Web/API/MessagePort">browser <code>MessagePort</code></a>s.</p> 8261cb0ef41Sopenharmony_ci<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> 8271cb0ef41Sopenharmony_ci<div class="api_metadata"> 8281cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 8291cb0ef41Sopenharmony_ci</div> 8301cb0ef41Sopenharmony_ci<p>The <code>'close'</code> event is emitted once either side of the channel has been 8311cb0ef41Sopenharmony_cidisconnected.</p> 8321cb0ef41Sopenharmony_ci<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>); 8331cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 8341cb0ef41Sopenharmony_ci 8351cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 8361cb0ef41Sopenharmony_ci<span class="hljs-comment">// foobar</span> 8371cb0ef41Sopenharmony_ci<span class="hljs-comment">// closed!</span> 8381cb0ef41Sopenharmony_ciport2.<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)); 8391cb0ef41Sopenharmony_ciport2.<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>)); 8401cb0ef41Sopenharmony_ci 8411cb0ef41Sopenharmony_ciport1.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'foobar'</span>); 8421cb0ef41Sopenharmony_ciport1.<span class="hljs-title function_">close</span>();</code> <button class="copy-button">copy</button></pre> 8431cb0ef41Sopenharmony_ci<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> 8441cb0ef41Sopenharmony_ci<div class="api_metadata"> 8451cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 8461cb0ef41Sopenharmony_ci</div> 8471cb0ef41Sopenharmony_ci<ul> 8481cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The transmitted value</li> 8491cb0ef41Sopenharmony_ci</ul> 8501cb0ef41Sopenharmony_ci<p>The <code>'message'</code> event is emitted for any incoming message, containing the cloned 8511cb0ef41Sopenharmony_ciinput of <a href="#portpostmessagevalue-transferlist"><code>port.postMessage()</code></a>.</p> 8521cb0ef41Sopenharmony_ci<p>Listeners on this event receive a clone of the <code>value</code> parameter as passed 8531cb0ef41Sopenharmony_cito <code>postMessage()</code> and no further arguments.</p> 8541cb0ef41Sopenharmony_ci<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> 8551cb0ef41Sopenharmony_ci<div class="api_metadata"> 8561cb0ef41Sopenharmony_ci<span>Added in: v14.5.0, v12.19.0</span> 8571cb0ef41Sopenharmony_ci</div> 8581cb0ef41Sopenharmony_ci<ul> 8591cb0ef41Sopenharmony_ci<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> An Error object</li> 8601cb0ef41Sopenharmony_ci</ul> 8611cb0ef41Sopenharmony_ci<p>The <code>'messageerror'</code> event is emitted when deserializing a message failed.</p> 8621cb0ef41Sopenharmony_ci<p>Currently, this event is emitted when there is an error occurring while 8631cb0ef41Sopenharmony_ciinstantiating the posted JS object on the receiving end. Such situations 8641cb0ef41Sopenharmony_ciare rare, but can happen, for instance, when certain Node.js API objects 8651cb0ef41Sopenharmony_ciare received in a <code>vm.Context</code> (where Node.js APIs are currently 8661cb0ef41Sopenharmony_ciunavailable).</p> 8671cb0ef41Sopenharmony_ci<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> 8681cb0ef41Sopenharmony_ci<div class="api_metadata"> 8691cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 8701cb0ef41Sopenharmony_ci</div> 8711cb0ef41Sopenharmony_ci<p>Disables further sending of messages on either side of the connection. 8721cb0ef41Sopenharmony_ciThis method can be called when no further communication will happen over this 8731cb0ef41Sopenharmony_ci<code>MessagePort</code>.</p> 8741cb0ef41Sopenharmony_ci<p>The <a href="#event-close"><code>'close'</code> event</a> is emitted on both <code>MessagePort</code> instances that 8751cb0ef41Sopenharmony_ciare part of the channel.</p> 8761cb0ef41Sopenharmony_ci<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> 8771cb0ef41Sopenharmony_ci<div class="api_metadata"> 8781cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 8791cb0ef41Sopenharmony_ci<table> 8801cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 8811cb0ef41Sopenharmony_ci<tr><td>v15.6.0</td> 8821cb0ef41Sopenharmony_ci<td><p>Added <code>X509Certificate</code> to the list of cloneable types.</p></td></tr> 8831cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 8841cb0ef41Sopenharmony_ci<td><p>Added <code>CryptoKey</code> to the list of cloneable types.</p></td></tr> 8851cb0ef41Sopenharmony_ci<tr><td>v15.14.0, v14.18.0</td> 8861cb0ef41Sopenharmony_ci<td><p>Add 'BlockList' to the list of cloneable types.</p></td></tr> 8871cb0ef41Sopenharmony_ci<tr><td>v15.9.0, v14.18.0</td> 8881cb0ef41Sopenharmony_ci<td><p>Add 'Histogram' types to the list of cloneable types.</p></td></tr> 8891cb0ef41Sopenharmony_ci<tr><td>v14.5.0, v12.19.0</td> 8901cb0ef41Sopenharmony_ci<td><p>Added <code>KeyObject</code> to the list of cloneable types.</p></td></tr> 8911cb0ef41Sopenharmony_ci<tr><td>v14.5.0, v12.19.0</td> 8921cb0ef41Sopenharmony_ci<td><p>Added <code>FileHandle</code> to the list of transferable types.</p></td></tr> 8931cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td> 8941cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.5.0</span></p></td></tr> 8951cb0ef41Sopenharmony_ci</tbody></table> 8961cb0ef41Sopenharmony_ci</details> 8971cb0ef41Sopenharmony_ci</div> 8981cb0ef41Sopenharmony_ci<ul> 8991cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 9001cb0ef41Sopenharmony_ci<li><code>transferList</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object[]></a></li> 9011cb0ef41Sopenharmony_ci</ul> 9021cb0ef41Sopenharmony_ci<p>Sends a JavaScript value to the receiving side of this channel. 9031cb0ef41Sopenharmony_ci<code>value</code> is transferred in a way which is compatible with 9041cb0ef41Sopenharmony_cithe <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm">HTML structured clone algorithm</a>.</p> 9051cb0ef41Sopenharmony_ci<p>In particular, the significant differences to <code>JSON</code> are:</p> 9061cb0ef41Sopenharmony_ci<ul> 9071cb0ef41Sopenharmony_ci<li><code>value</code> may contain circular references.</li> 9081cb0ef41Sopenharmony_ci<li><code>value</code> may contain instances of builtin JS types such as <code>RegExp</code>s, 9091cb0ef41Sopenharmony_ci<code>BigInt</code>s, <code>Map</code>s, <code>Set</code>s, etc.</li> 9101cb0ef41Sopenharmony_ci<li><code>value</code> may contain typed arrays, both using <code>ArrayBuffer</code>s 9111cb0ef41Sopenharmony_ciand <code>SharedArrayBuffer</code>s.</li> 9121cb0ef41Sopenharmony_ci<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> 9131cb0ef41Sopenharmony_ci<li><code>value</code> may not contain native (C++-backed) objects other than: 9141cb0ef41Sopenharmony_ci<ul> 9151cb0ef41Sopenharmony_ci<li><a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a>s,</li> 9161cb0ef41Sopenharmony_ci<li><a href="fs.html#class-filehandle" class="type"><FileHandle></a>s,</li> 9171cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html#class-histogram" class="type"><Histogram></a>s,</li> 9181cb0ef41Sopenharmony_ci<li><a href="crypto.html#class-keyobject" class="type"><KeyObject></a>s,</li> 9191cb0ef41Sopenharmony_ci<li><a href="worker_threads.html#class-messageport" class="type"><MessagePort></a>s,</li> 9201cb0ef41Sopenharmony_ci<li><a href="net.html#class-netblocklist" class="type"><net.BlockList></a>s,</li> 9211cb0ef41Sopenharmony_ci<li><a href="net.html#class-netsocketaddress" class="type"><net.SocketAddress></a>es,</li> 9221cb0ef41Sopenharmony_ci<li><a href="crypto.html#class-x509certificate" class="type"><X509Certificate></a>s.</li> 9231cb0ef41Sopenharmony_ci</ul> 9241cb0ef41Sopenharmony_ci</li> 9251cb0ef41Sopenharmony_ci</ul> 9261cb0ef41Sopenharmony_ci<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>); 9271cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 9281cb0ef41Sopenharmony_ci 9291cb0ef41Sopenharmony_ciport1.<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)); 9301cb0ef41Sopenharmony_ci 9311cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> circularData = {}; 9321cb0ef41Sopenharmony_cicircularData.<span class="hljs-property">foo</span> = circularData; 9331cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: { foo: [Circular] }</span> 9341cb0ef41Sopenharmony_ciport2.<span class="hljs-title function_">postMessage</span>(circularData);</code> <button class="copy-button">copy</button></pre> 9351cb0ef41Sopenharmony_ci<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 9361cb0ef41Sopenharmony_ci<a href="fs.html#class-filehandle"><code>FileHandle</code></a> objects. 9371cb0ef41Sopenharmony_ciAfter transferring, they are not usable on the sending side of the channel 9381cb0ef41Sopenharmony_cianymore (even if they are not contained in <code>value</code>). Unlike with 9391cb0ef41Sopenharmony_ci<a href="child_process.html">child processes</a>, transferring handles such as network sockets is currently 9401cb0ef41Sopenharmony_cinot supported.</p> 9411cb0ef41Sopenharmony_ci<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 9421cb0ef41Sopenharmony_cifrom either thread. They cannot be listed in <code>transferList</code>.</p> 9431cb0ef41Sopenharmony_ci<p><code>value</code> may still contain <code>ArrayBuffer</code> instances that are not in 9441cb0ef41Sopenharmony_ci<code>transferList</code>; in that case, the underlying memory is copied rather than moved.</p> 9451cb0ef41Sopenharmony_ci<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>); 9461cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 9471cb0ef41Sopenharmony_ci 9481cb0ef41Sopenharmony_ciport1.<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)); 9491cb0ef41Sopenharmony_ci 9501cb0ef41Sopenharmony_ci<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> ]); 9511cb0ef41Sopenharmony_ci<span class="hljs-comment">// This posts a copy of `uint8Array`:</span> 9521cb0ef41Sopenharmony_ciport2.<span class="hljs-title function_">postMessage</span>(uint8Array); 9531cb0ef41Sopenharmony_ci<span class="hljs-comment">// This does not copy data, but renders `uint8Array` unusable:</span> 9541cb0ef41Sopenharmony_ciport2.<span class="hljs-title function_">postMessage</span>(uint8Array, [ uint8Array.<span class="hljs-property">buffer</span> ]); 9551cb0ef41Sopenharmony_ci 9561cb0ef41Sopenharmony_ci<span class="hljs-comment">// The memory for the `sharedUint8Array` is accessible from both the</span> 9571cb0ef41Sopenharmony_ci<span class="hljs-comment">// original and the copy received by `.on('message')`:</span> 9581cb0ef41Sopenharmony_ci<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>)); 9591cb0ef41Sopenharmony_ciport2.<span class="hljs-title function_">postMessage</span>(sharedUint8Array); 9601cb0ef41Sopenharmony_ci 9611cb0ef41Sopenharmony_ci<span class="hljs-comment">// This transfers a freshly created message port to the receiver.</span> 9621cb0ef41Sopenharmony_ci<span class="hljs-comment">// This can be used, for example, to create communication channels between</span> 9631cb0ef41Sopenharmony_ci<span class="hljs-comment">// multiple `Worker` threads that are children of the same parent thread.</span> 9641cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> otherChannel = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 9651cb0ef41Sopenharmony_ciport2.<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> 9661cb0ef41Sopenharmony_ci<p>The message object is cloned immediately, and can be modified after 9671cb0ef41Sopenharmony_ciposting without having side effects.</p> 9681cb0ef41Sopenharmony_ci<p>For more information on the serialization and deserialization mechanisms 9691cb0ef41Sopenharmony_cibehind this API, see the <a href="v8.html#serialization-api">serialization API of the <code>node:v8</code> module</a>.</p> 9701cb0ef41Sopenharmony_ci<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> 9711cb0ef41Sopenharmony_ci<p>All <code>TypedArray</code> and <code>Buffer</code> instances are views over an underlying 9721cb0ef41Sopenharmony_ci<code>ArrayBuffer</code>. That is, it is the <code>ArrayBuffer</code> that actually stores 9731cb0ef41Sopenharmony_cithe raw data while the <code>TypedArray</code> and <code>Buffer</code> objects provide a 9741cb0ef41Sopenharmony_ciway of viewing and manipulating the data. It is possible and common 9751cb0ef41Sopenharmony_cifor multiple views to be created over the same <code>ArrayBuffer</code> instance. 9761cb0ef41Sopenharmony_ciGreat care must be taken when using a transfer list to transfer an 9771cb0ef41Sopenharmony_ci<code>ArrayBuffer</code> as doing so causes all <code>TypedArray</code> and <code>Buffer</code> 9781cb0ef41Sopenharmony_ciinstances that share that same <code>ArrayBuffer</code> to become unusable.</p> 9791cb0ef41Sopenharmony_ci<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>); 9801cb0ef41Sopenharmony_ci 9811cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> u1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(ab); 9821cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> u2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(ab); 9831cb0ef41Sopenharmony_ci 9841cb0ef41Sopenharmony_ci<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> 9851cb0ef41Sopenharmony_ci 9861cb0ef41Sopenharmony_ciport.<span class="hljs-title function_">postMessage</span>(u1, [u1.<span class="hljs-property">buffer</span>]); 9871cb0ef41Sopenharmony_ci 9881cb0ef41Sopenharmony_ci<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> 9891cb0ef41Sopenharmony_ci<p>For <code>Buffer</code> instances, specifically, whether the underlying 9901cb0ef41Sopenharmony_ci<code>ArrayBuffer</code> can be transferred or cloned depends entirely on how 9911cb0ef41Sopenharmony_ciinstances were created, which often cannot be reliably determined.</p> 9921cb0ef41Sopenharmony_ci<p>An <code>ArrayBuffer</code> can be marked with <a href="#workermarkasuntransferableobject"><code>markAsUntransferable()</code></a> to indicate 9931cb0ef41Sopenharmony_cithat it should always be cloned and never transferred.</p> 9941cb0ef41Sopenharmony_ci<p>Depending on how a <code>Buffer</code> instance was created, it may or may 9951cb0ef41Sopenharmony_cinot own its underlying <code>ArrayBuffer</code>. An <code>ArrayBuffer</code> must not 9961cb0ef41Sopenharmony_cibe transferred unless it is known that the <code>Buffer</code> instance 9971cb0ef41Sopenharmony_ciowns it. In particular, for <code>Buffer</code>s created from the internal 9981cb0ef41Sopenharmony_ci<code>Buffer</code> pool (using, for instance <code>Buffer.from()</code> or <code>Buffer.allocUnsafe()</code>), 9991cb0ef41Sopenharmony_citransferring them is not possible and they are always cloned, 10001cb0ef41Sopenharmony_ciwhich sends a copy of the entire <code>Buffer</code> pool. 10011cb0ef41Sopenharmony_ciThis behavior may come with unintended higher memory 10021cb0ef41Sopenharmony_ciusage and possible security concerns.</p> 10031cb0ef41Sopenharmony_ci<p>See <a href="buffer.html#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> for more details on <code>Buffer</code> pooling.</p> 10041cb0ef41Sopenharmony_ci<p>The <code>ArrayBuffer</code>s for <code>Buffer</code> instances created using 10051cb0ef41Sopenharmony_ci<code>Buffer.alloc()</code> or <code>Buffer.allocUnsafeSlow()</code> can always be 10061cb0ef41Sopenharmony_citransferred but doing so renders all other existing views of 10071cb0ef41Sopenharmony_cithose <code>ArrayBuffer</code>s unusable.</p> 10081cb0ef41Sopenharmony_ci<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> 10091cb0ef41Sopenharmony_ci<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>, 10101cb0ef41Sopenharmony_cinon-enumerable properties, property accessors, and object prototypes are 10111cb0ef41Sopenharmony_cinot preserved. In particular, <a href="buffer.html"><code>Buffer</code></a> objects will be read as 10121cb0ef41Sopenharmony_ciplain <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 10131cb0ef41Sopenharmony_ciclasses will be cloned as plain JavaScript objects.</p> 10141cb0ef41Sopenharmony_ci<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>); 10151cb0ef41Sopenharmony_ci 10161cb0ef41Sopenharmony_ci<span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> { 10171cb0ef41Sopenharmony_ci #a = <span class="hljs-number">1</span>; 10181cb0ef41Sopenharmony_ci <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) { 10191cb0ef41Sopenharmony_ci <span class="hljs-variable language_">this</span>[b] = <span class="hljs-number">2</span>; 10201cb0ef41Sopenharmony_ci <span class="hljs-variable language_">this</span>.<span class="hljs-property">c</span> = <span class="hljs-number">3</span>; 10211cb0ef41Sopenharmony_ci } 10221cb0ef41Sopenharmony_ci 10231cb0ef41Sopenharmony_ci <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>; } 10241cb0ef41Sopenharmony_ci} 10251cb0ef41Sopenharmony_ci 10261cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 10271cb0ef41Sopenharmony_ci 10281cb0ef41Sopenharmony_ciport1.<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); 10291cb0ef41Sopenharmony_ci 10301cb0ef41Sopenharmony_ciport2.<span class="hljs-title function_">postMessage</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Foo</span>()); 10311cb0ef41Sopenharmony_ci 10321cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: { c: 3 }</span></code> <button class="copy-button">copy</button></pre> 10331cb0ef41Sopenharmony_ci<p>This limitation extends to many built-in objects, such as the global <code>URL</code> 10341cb0ef41Sopenharmony_ciobject:</p> 10351cb0ef41Sopenharmony_ci<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>(); 10361cb0ef41Sopenharmony_ci 10371cb0ef41Sopenharmony_ciport1.<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); 10381cb0ef41Sopenharmony_ci 10391cb0ef41Sopenharmony_ciport2.<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>)); 10401cb0ef41Sopenharmony_ci 10411cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: { }</span></code> <button class="copy-button">copy</button></pre> 10421cb0ef41Sopenharmony_ci<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> 10431cb0ef41Sopenharmony_ci<div class="api_metadata"> 10441cb0ef41Sopenharmony_ci<span>Added in: v18.1.0</span> 10451cb0ef41Sopenharmony_ci</div> 10461cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 10471cb0ef41Sopenharmony_ci<ul> 10481cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 10491cb0ef41Sopenharmony_ci</ul> 10501cb0ef41Sopenharmony_ci<p>If true, the <code>MessagePort</code> object will keep the Node.js event loop active.</p> 10511cb0ef41Sopenharmony_ci<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> 10521cb0ef41Sopenharmony_ci<div class="api_metadata"> 10531cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 10541cb0ef41Sopenharmony_ci</div> 10551cb0ef41Sopenharmony_ci<p>Opposite of <code>unref()</code>. Calling <code>ref()</code> on a previously <code>unref()</code>ed port does 10561cb0ef41Sopenharmony_ci<em>not</em> let the program exit if it's the only active handle left (the default 10571cb0ef41Sopenharmony_cibehavior). If the port is <code>ref()</code>ed, calling <code>ref()</code> again has no effect.</p> 10581cb0ef41Sopenharmony_ci<p>If listeners are attached or removed using <code>.on('message')</code>, the port 10591cb0ef41Sopenharmony_ciis <code>ref()</code>ed and <code>unref()</code>ed automatically depending on whether 10601cb0ef41Sopenharmony_cilisteners for the event exist.</p> 10611cb0ef41Sopenharmony_ci<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> 10621cb0ef41Sopenharmony_ci<div class="api_metadata"> 10631cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 10641cb0ef41Sopenharmony_ci</div> 10651cb0ef41Sopenharmony_ci<p>Starts receiving messages on this <code>MessagePort</code>. When using this port 10661cb0ef41Sopenharmony_cias an event emitter, this is called automatically once <code>'message'</code> 10671cb0ef41Sopenharmony_cilisteners are attached.</p> 10681cb0ef41Sopenharmony_ci<p>This method exists for parity with the Web <code>MessagePort</code> API. In Node.js, 10691cb0ef41Sopenharmony_ciit is only useful for ignoring messages when no event listener is present. 10701cb0ef41Sopenharmony_ciNode.js also diverges in its handling of <code>.onmessage</code>. Setting it 10711cb0ef41Sopenharmony_ciautomatically calls <code>.start()</code>, but unsetting it lets messages queue up 10721cb0ef41Sopenharmony_ciuntil a new handler is set or the port is discarded.</p> 10731cb0ef41Sopenharmony_ci<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> 10741cb0ef41Sopenharmony_ci<div class="api_metadata"> 10751cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 10761cb0ef41Sopenharmony_ci</div> 10771cb0ef41Sopenharmony_ci<p>Calling <code>unref()</code> on a port allows the thread to exit if this is the only 10781cb0ef41Sopenharmony_ciactive handle in the event system. If the port is already <code>unref()</code>ed calling 10791cb0ef41Sopenharmony_ci<code>unref()</code> again has no effect.</p> 10801cb0ef41Sopenharmony_ci<p>If listeners are attached or removed using <code>.on('message')</code>, the port is 10811cb0ef41Sopenharmony_ci<code>ref()</code>ed and <code>unref()</code>ed automatically depending on whether 10821cb0ef41Sopenharmony_cilisteners for the event exist.</p> 10831cb0ef41Sopenharmony_ci</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> 10841cb0ef41Sopenharmony_ci<div class="api_metadata"> 10851cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 10861cb0ef41Sopenharmony_ci</div> 10871cb0ef41Sopenharmony_ci<ul> 10881cb0ef41Sopenharmony_ci<li>Extends: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 10891cb0ef41Sopenharmony_ci</ul> 10901cb0ef41Sopenharmony_ci<p>The <code>Worker</code> class represents an independent JavaScript execution thread. 10911cb0ef41Sopenharmony_ciMost Node.js APIs are available inside of it.</p> 10921cb0ef41Sopenharmony_ci<p>Notable differences inside a Worker environment are:</p> 10931cb0ef41Sopenharmony_ci<ul> 10941cb0ef41Sopenharmony_ci<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> 10951cb0ef41Sopenharmony_cistreams may be redirected by the parent thread.</li> 10961cb0ef41Sopenharmony_ci<li>The <a href="#workerismainthread"><code>require('node:worker_threads').isMainThread</code></a> property is set to <code>false</code>.</li> 10971cb0ef41Sopenharmony_ci<li>The <a href="#workerparentport"><code>require('node:worker_threads').parentPort</code></a> message port is available.</li> 10981cb0ef41Sopenharmony_ci<li><a href="process.html#processexitcode"><code>process.exit()</code></a> does not stop the whole program, just the single thread, 10991cb0ef41Sopenharmony_ciand <a href="process.html#processabort"><code>process.abort()</code></a> is not available.</li> 11001cb0ef41Sopenharmony_ci<li><a href="process.html#processchdirdirectory"><code>process.chdir()</code></a> and <code>process</code> methods that set group or user ids 11011cb0ef41Sopenharmony_ciare not available.</li> 11021cb0ef41Sopenharmony_ci<li><a href="process.html#processenv"><code>process.env</code></a> is a copy of the parent thread's environment variables, 11031cb0ef41Sopenharmony_ciunless otherwise specified. Changes to one copy are not visible in other 11041cb0ef41Sopenharmony_cithreads, and are not visible to native add-ons (unless 11051cb0ef41Sopenharmony_ci<a href="#workershare_env"><code>worker.SHARE_ENV</code></a> is passed as the <code>env</code> option to the 11061cb0ef41Sopenharmony_ci<a href="#class-worker"><code>Worker</code></a> constructor). On Windows, unlike the main thread, a copy of the 11071cb0ef41Sopenharmony_cienvironment variables operates in a case-sensitive manner.</li> 11081cb0ef41Sopenharmony_ci<li><a href="process.html#processtitle"><code>process.title</code></a> cannot be modified.</li> 11091cb0ef41Sopenharmony_ci<li>Signals are not delivered through <a href="process.html#signal-events"><code>process.on('...')</code></a>.</li> 11101cb0ef41Sopenharmony_ci<li>Execution may stop at any point as a result of <a href="#workerterminate"><code>worker.terminate()</code></a> 11111cb0ef41Sopenharmony_cibeing invoked.</li> 11121cb0ef41Sopenharmony_ci<li>IPC channels from parent processes are not accessible.</li> 11131cb0ef41Sopenharmony_ci<li>The <a href="tracing.html"><code>trace_events</code></a> module is not supported.</li> 11141cb0ef41Sopenharmony_ci<li>Native add-ons can only be loaded from multiple threads if they fulfill 11151cb0ef41Sopenharmony_ci<a href="addons.html#worker-support">certain conditions</a>.</li> 11161cb0ef41Sopenharmony_ci</ul> 11171cb0ef41Sopenharmony_ci<p>Creating <code>Worker</code> instances inside of other <code>Worker</code>s is possible.</p> 11181cb0ef41Sopenharmony_ci<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 11191cb0ef41Sopenharmony_cican be achieved through inter-thread message passing. Internally, a <code>Worker</code> has 11201cb0ef41Sopenharmony_cia built-in pair of <a href="#class-messageport"><code>MessagePort</code></a>s that are already associated with each 11211cb0ef41Sopenharmony_ciother when the <code>Worker</code> is created. While the <code>MessagePort</code> object on the parent 11221cb0ef41Sopenharmony_ciside is not directly exposed, its functionalities are exposed through 11231cb0ef41Sopenharmony_ci<a href="#workerpostmessagevalue-transferlist"><code>worker.postMessage()</code></a> and the <a href="#event-message_1"><code>worker.on('message')</code></a> event 11241cb0ef41Sopenharmony_cion the <code>Worker</code> object for the parent thread.</p> 11251cb0ef41Sopenharmony_ci<p>To create custom messaging channels (which is encouraged over using the default 11261cb0ef41Sopenharmony_ciglobal channel because it facilitates separation of concerns), users can create 11271cb0ef41Sopenharmony_cia <code>MessageChannel</code> object on either thread and pass one of the 11281cb0ef41Sopenharmony_ci<code>MessagePort</code>s on that <code>MessageChannel</code> to the other thread through a 11291cb0ef41Sopenharmony_cipre-existing channel, such as the global one.</p> 11301cb0ef41Sopenharmony_ci<p>See <a href="#portpostmessagevalue-transferlist"><code>port.postMessage()</code></a> for more information on how messages are passed, 11311cb0ef41Sopenharmony_ciand what kind of JavaScript values can be successfully transported through 11321cb0ef41Sopenharmony_cithe thread barrier.</p> 11331cb0ef41Sopenharmony_ci<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>); 11341cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 11351cb0ef41Sopenharmony_ci <span class="hljs-title class_">Worker</span>, <span class="hljs-title class_">MessageChannel</span>, <span class="hljs-title class_">MessagePort</span>, isMainThread, parentPort, 11361cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>); 11371cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (isMainThread) { 11381cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename); 11391cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> subChannel = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 11401cb0ef41Sopenharmony_ci 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>]); 11411cb0ef41Sopenharmony_ci 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> { 11421cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'received:'</span>, value); 11431cb0ef41Sopenharmony_ci }); 11441cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> { 11451cb0ef41Sopenharmony_ci 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> { 11461cb0ef41Sopenharmony_ci <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>); 11471cb0ef41Sopenharmony_ci value.<span class="hljs-property">hereIsYourPort</span>.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'the worker is sending this'</span>); 11481cb0ef41Sopenharmony_ci value.<span class="hljs-property">hereIsYourPort</span>.<span class="hljs-title function_">close</span>(); 11491cb0ef41Sopenharmony_ci }); 11501cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 11511cb0ef41Sopenharmony_ci<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> 11521cb0ef41Sopenharmony_ci<div class="api_metadata"> 11531cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 11541cb0ef41Sopenharmony_ci<table> 11551cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 11561cb0ef41Sopenharmony_ci<tr><td>v18.16.0</td> 11571cb0ef41Sopenharmony_ci<td><p>Added support for a <code>name</code> option, which allows adding a name to worker title for debugging.</p></td></tr> 11581cb0ef41Sopenharmony_ci<tr><td>v14.9.0</td> 11591cb0ef41Sopenharmony_ci<td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>data:</code> protocol.</p></td></tr> 11601cb0ef41Sopenharmony_ci<tr><td>v14.9.0</td> 11611cb0ef41Sopenharmony_ci<td><p>The <code>trackUnmanagedFds</code> option was set to <code>true</code> by default.</p></td></tr> 11621cb0ef41Sopenharmony_ci<tr><td>v14.6.0, v12.19.0</td> 11631cb0ef41Sopenharmony_ci<td><p>The <code>trackUnmanagedFds</code> option was introduced.</p></td></tr> 11641cb0ef41Sopenharmony_ci<tr><td>v13.13.0, v12.17.0</td> 11651cb0ef41Sopenharmony_ci<td><p>The <code>transferList</code> option was introduced.</p></td></tr> 11661cb0ef41Sopenharmony_ci<tr><td>v13.12.0, v12.17.0</td> 11671cb0ef41Sopenharmony_ci<td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 11681cb0ef41Sopenharmony_ci<tr><td>v13.4.0, v12.16.0</td> 11691cb0ef41Sopenharmony_ci<td><p>The <code>argv</code> option was introduced.</p></td></tr> 11701cb0ef41Sopenharmony_ci<tr><td>v13.2.0, v12.16.0</td> 11711cb0ef41Sopenharmony_ci<td><p>The <code>resourceLimits</code> option was introduced.</p></td></tr> 11721cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td> 11731cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.5.0</span></p></td></tr> 11741cb0ef41Sopenharmony_ci</tbody></table> 11751cb0ef41Sopenharmony_ci</details> 11761cb0ef41Sopenharmony_ci</div> 11771cb0ef41Sopenharmony_ci<ul> 11781cb0ef41Sopenharmony_ci<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> The path to the Worker's main script or module. Must 11791cb0ef41Sopenharmony_cibe either an absolute path or a relative path (i.e. relative to the 11801cb0ef41Sopenharmony_cicurrent working directory) starting with <code>./</code> or <code>../</code>, or a WHATWG <code>URL</code> 11811cb0ef41Sopenharmony_ciobject using <code>file:</code> or <code>data:</code> protocol. 11821cb0ef41Sopenharmony_ciWhen 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 11831cb0ef41Sopenharmony_cithe <a href="esm.html#data-imports">ECMAScript module loader</a>. 11841cb0ef41Sopenharmony_ciIf <code>options.eval</code> is <code>true</code>, this is a string containing JavaScript code 11851cb0ef41Sopenharmony_cirather than a path.</li> 11861cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 11871cb0ef41Sopenharmony_ci<ul> 11881cb0ef41Sopenharmony_ci<li><code>argv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any[]></a> List of arguments which would be stringified and appended to 11891cb0ef41Sopenharmony_ci<code>process.argv</code> in the worker. This is mostly similar to the <code>workerData</code> 11901cb0ef41Sopenharmony_cibut the values are available on the global <code>process.argv</code> as if they 11911cb0ef41Sopenharmony_ciwere passed as CLI options to the script.</li> 11921cb0ef41Sopenharmony_ci<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> If set, specifies the initial value of <code>process.env</code> inside 11931cb0ef41Sopenharmony_cithe Worker thread. As a special value, <a href="#workershare_env"><code>worker.SHARE_ENV</code></a> may be used 11941cb0ef41Sopenharmony_cito specify that the parent thread and the child thread should share their 11951cb0ef41Sopenharmony_cienvironment variables; in that case, changes to one thread's <code>process.env</code> 11961cb0ef41Sopenharmony_ciobject affect the other thread as well. <strong>Default:</strong> <code>process.env</code>.</li> 11971cb0ef41Sopenharmony_ci<li><code>eval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code> and the first argument is a <code>string</code>, interpret 11981cb0ef41Sopenharmony_cithe first argument to the constructor as a script that is executed once the 11991cb0ef41Sopenharmony_ciworker is online.</li> 12001cb0ef41Sopenharmony_ci<li><code>execArgv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> List of node CLI options passed to the worker. 12011cb0ef41Sopenharmony_ciV8 options (such as <code>--max-old-space-size</code>) and options that affect the 12021cb0ef41Sopenharmony_ciprocess (such as <code>--title</code>) are not supported. If set, this is provided 12031cb0ef41Sopenharmony_cias <a href="process.html#processexecargv"><code>process.execArgv</code></a> inside the worker. By default, options are 12041cb0ef41Sopenharmony_ciinherited from the parent thread.</li> 12051cb0ef41Sopenharmony_ci<li><code>stdin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If this is set to <code>true</code>, then <code>worker.stdin</code> 12061cb0ef41Sopenharmony_ciprovides a writable stream whose contents appear as <code>process.stdin</code> 12071cb0ef41Sopenharmony_ciinside the Worker. By default, no data is provided.</li> 12081cb0ef41Sopenharmony_ci<li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If this is set to <code>true</code>, then <code>worker.stdout</code> is 12091cb0ef41Sopenharmony_cinot automatically piped through to <code>process.stdout</code> in the parent.</li> 12101cb0ef41Sopenharmony_ci<li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If this is set to <code>true</code>, then <code>worker.stderr</code> is 12111cb0ef41Sopenharmony_cinot automatically piped through to <code>process.stderr</code> in the parent.</li> 12121cb0ef41Sopenharmony_ci<li><code>workerData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Any JavaScript value that is cloned and made 12131cb0ef41Sopenharmony_ciavailable as <a href="#workerworkerdata"><code>require('node:worker_threads').workerData</code></a>. The cloning 12141cb0ef41Sopenharmony_cioccurs 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 12151cb0ef41Sopenharmony_ciis thrown if the object cannot be cloned (e.g. because it contains 12161cb0ef41Sopenharmony_ci<code>function</code>s).</li> 12171cb0ef41Sopenharmony_ci<li><code>trackUnmanagedFds</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If this is set to <code>true</code>, then the Worker 12181cb0ef41Sopenharmony_citracks raw file descriptors managed through <a href="fs.html#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> and 12191cb0ef41Sopenharmony_ci<a href="fs.html#fsclosefd-callback"><code>fs.close()</code></a>, and closes them when the Worker exits, similar to other 12201cb0ef41Sopenharmony_ciresources like network sockets or file descriptors managed through 12211cb0ef41Sopenharmony_cithe <a href="fs.html#class-filehandle"><code>FileHandle</code></a> API. This option is automatically inherited by all 12221cb0ef41Sopenharmony_cinested <code>Worker</code>s. <strong>Default:</strong> <code>true</code>.</li> 12231cb0ef41Sopenharmony_ci<li><code>transferList</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object[]></a> If one or more <code>MessagePort</code>-like objects 12241cb0ef41Sopenharmony_ciare passed in <code>workerData</code>, a <code>transferList</code> is required for those 12251cb0ef41Sopenharmony_ciitems or <a href="errors.html#err_missing_message_port_in_transfer_list"><code>ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST</code></a> is thrown. 12261cb0ef41Sopenharmony_ciSee <a href="#portpostmessagevalue-transferlist"><code>port.postMessage()</code></a> for more information.</li> 12271cb0ef41Sopenharmony_ci<li><code>resourceLimits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> An optional set of resource limits for the new JS 12281cb0ef41Sopenharmony_ciengine instance. Reaching these limits leads to termination of the <code>Worker</code> 12291cb0ef41Sopenharmony_ciinstance. These limits only affect the JS engine, and no external data, 12301cb0ef41Sopenharmony_ciincluding no <code>ArrayBuffer</code>s. Even if these limits are set, the process may 12311cb0ef41Sopenharmony_cistill abort if it encounters a global out-of-memory situation. 12321cb0ef41Sopenharmony_ci<ul> 12331cb0ef41Sopenharmony_ci<li><code>maxOldGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The maximum size of the main heap in 12341cb0ef41Sopenharmony_ciMB. 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 12351cb0ef41Sopenharmony_cioverrides this setting.</li> 12361cb0ef41Sopenharmony_ci<li><code>maxYoungGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The maximum size of a heap space for 12371cb0ef41Sopenharmony_cirecently created objects. If the command-line argument 12381cb0ef41Sopenharmony_ci<a href="cli.html#--max-semi-space-sizesize-in-megabytes"><code>--max-semi-space-size</code></a> is set, it overrides this setting.</li> 12391cb0ef41Sopenharmony_ci<li><code>codeRangeSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The size of a pre-allocated memory range 12401cb0ef41Sopenharmony_ciused for generated code.</li> 12411cb0ef41Sopenharmony_ci<li><code>stackSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The default maximum stack size for the thread. 12421cb0ef41Sopenharmony_ciSmall values may lead to unusable Worker instances. <strong>Default:</strong> <code>4</code>.</li> 12431cb0ef41Sopenharmony_ci</ul> 12441cb0ef41Sopenharmony_ci</li> 12451cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> An optional <code>name</code> to be appended to the worker title 12461cb0ef41Sopenharmony_cifor debugging/identification purposes, making the final title as 12471cb0ef41Sopenharmony_ci<code>[worker ${id}] ${name}</code>. <strong>Default:</strong> <code>''</code>.</li> 12481cb0ef41Sopenharmony_ci</ul> 12491cb0ef41Sopenharmony_ci</li> 12501cb0ef41Sopenharmony_ci</ul> 12511cb0ef41Sopenharmony_ci<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> 12521cb0ef41Sopenharmony_ci<div class="api_metadata"> 12531cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 12541cb0ef41Sopenharmony_ci</div> 12551cb0ef41Sopenharmony_ci<ul> 12561cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 12571cb0ef41Sopenharmony_ci</ul> 12581cb0ef41Sopenharmony_ci<p>The <code>'error'</code> event is emitted if the worker thread throws an uncaught 12591cb0ef41Sopenharmony_ciexception. In that case, the worker is terminated.</p> 12601cb0ef41Sopenharmony_ci<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> 12611cb0ef41Sopenharmony_ci<div class="api_metadata"> 12621cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 12631cb0ef41Sopenharmony_ci</div> 12641cb0ef41Sopenharmony_ci<ul> 12651cb0ef41Sopenharmony_ci<li><code>exitCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 12661cb0ef41Sopenharmony_ci</ul> 12671cb0ef41Sopenharmony_ci<p>The <code>'exit'</code> event is emitted once the worker has stopped. If the worker 12681cb0ef41Sopenharmony_ciexited by calling <a href="process.html#processexitcode"><code>process.exit()</code></a>, the <code>exitCode</code> parameter is the 12691cb0ef41Sopenharmony_cipassed exit code. If the worker was terminated, the <code>exitCode</code> parameter is 12701cb0ef41Sopenharmony_ci<code>1</code>.</p> 12711cb0ef41Sopenharmony_ci<p>This is the final event emitted by any <code>Worker</code> instance.</p> 12721cb0ef41Sopenharmony_ci<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> 12731cb0ef41Sopenharmony_ci<div class="api_metadata"> 12741cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 12751cb0ef41Sopenharmony_ci</div> 12761cb0ef41Sopenharmony_ci<ul> 12771cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The transmitted value</li> 12781cb0ef41Sopenharmony_ci</ul> 12791cb0ef41Sopenharmony_ci<p>The <code>'message'</code> event is emitted when the worker thread has invoked 12801cb0ef41Sopenharmony_ci<a href="#workerpostmessagevalue-transferlist"><code>require('node:worker_threads').parentPort.postMessage()</code></a>. 12811cb0ef41Sopenharmony_ciSee the <a href="#event-message"><code>port.on('message')</code></a> event for more details.</p> 12821cb0ef41Sopenharmony_ci<p>All messages sent from the worker thread are emitted before the 12831cb0ef41Sopenharmony_ci<a href="#event-exit"><code>'exit'</code> event</a> is emitted on the <code>Worker</code> object.</p> 12841cb0ef41Sopenharmony_ci<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> 12851cb0ef41Sopenharmony_ci<div class="api_metadata"> 12861cb0ef41Sopenharmony_ci<span>Added in: v14.5.0, v12.19.0</span> 12871cb0ef41Sopenharmony_ci</div> 12881cb0ef41Sopenharmony_ci<ul> 12891cb0ef41Sopenharmony_ci<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> An Error object</li> 12901cb0ef41Sopenharmony_ci</ul> 12911cb0ef41Sopenharmony_ci<p>The <code>'messageerror'</code> event is emitted when deserializing a message failed.</p> 12921cb0ef41Sopenharmony_ci<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> 12931cb0ef41Sopenharmony_ci<div class="api_metadata"> 12941cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 12951cb0ef41Sopenharmony_ci</div> 12961cb0ef41Sopenharmony_ci<p>The <code>'online'</code> event is emitted when the worker thread has started executing 12971cb0ef41Sopenharmony_ciJavaScript code.</p> 12981cb0ef41Sopenharmony_ci<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> 12991cb0ef41Sopenharmony_ci<div class="api_metadata"> 13001cb0ef41Sopenharmony_ci<span>Added in: v13.9.0, v12.17.0</span> 13011cb0ef41Sopenharmony_ci</div> 13021cb0ef41Sopenharmony_ci<ul> 13031cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> A promise for a Readable Stream containing 13041cb0ef41Sopenharmony_cia V8 heap snapshot</li> 13051cb0ef41Sopenharmony_ci</ul> 13061cb0ef41Sopenharmony_ci<p>Returns a readable stream for a V8 snapshot of the current state of the Worker. 13071cb0ef41Sopenharmony_ciSee <a href="v8.html#v8getheapsnapshot"><code>v8.getHeapSnapshot()</code></a> for more details.</p> 13081cb0ef41Sopenharmony_ci<p>If the Worker thread is no longer running, which may occur before the 13091cb0ef41Sopenharmony_ci<a href="#event-exit"><code>'exit'</code> event</a> is emitted, the returned <code>Promise</code> is rejected 13101cb0ef41Sopenharmony_ciimmediately with an <a href="errors.html#err_worker_not_running"><code>ERR_WORKER_NOT_RUNNING</code></a> error.</p> 13111cb0ef41Sopenharmony_ci<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> 13121cb0ef41Sopenharmony_ci<div class="api_metadata"> 13131cb0ef41Sopenharmony_ci<span>Added in: v15.1.0, v14.17.0, v12.22.0</span> 13141cb0ef41Sopenharmony_ci</div> 13151cb0ef41Sopenharmony_ci<p>An object that can be used to query performance information from a worker 13161cb0ef41Sopenharmony_ciinstance. Similar to <a href="perf_hooks.html#perf_hooksperformance"><code>perf_hooks.performance</code></a>.</p> 13171cb0ef41Sopenharmony_ci<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> 13181cb0ef41Sopenharmony_ci<div class="api_metadata"> 13191cb0ef41Sopenharmony_ci<span>Added in: v15.1.0, v14.17.0, v12.22.0</span> 13201cb0ef41Sopenharmony_ci</div> 13211cb0ef41Sopenharmony_ci<ul> 13221cb0ef41Sopenharmony_ci<li><code>utilization1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> The result of a previous call to 13231cb0ef41Sopenharmony_ci<code>eventLoopUtilization()</code>.</li> 13241cb0ef41Sopenharmony_ci<li><code>utilization2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> The result of a previous call to 13251cb0ef41Sopenharmony_ci<code>eventLoopUtilization()</code> prior to <code>utilization1</code>.</li> 13261cb0ef41Sopenharmony_ci<li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 13271cb0ef41Sopenharmony_ci<ul> 13281cb0ef41Sopenharmony_ci<li><code>idle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 13291cb0ef41Sopenharmony_ci<li><code>active</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 13301cb0ef41Sopenharmony_ci<li><code>utilization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 13311cb0ef41Sopenharmony_ci</ul> 13321cb0ef41Sopenharmony_ci</li> 13331cb0ef41Sopenharmony_ci</ul> 13341cb0ef41Sopenharmony_ci<p>The same call as <a href="perf_hooks.html#performanceeventlooputilizationutilization1-utilization2"><code>perf_hooks</code> <code>eventLoopUtilization()</code></a>, except the values 13351cb0ef41Sopenharmony_ciof the worker instance are returned.</p> 13361cb0ef41Sopenharmony_ci<p>One difference is that, unlike the main thread, bootstrapping within a worker 13371cb0ef41Sopenharmony_ciis done within the event loop. So the event loop utilization is 13381cb0ef41Sopenharmony_ciimmediately available once the worker's script begins execution.</p> 13391cb0ef41Sopenharmony_ci<p>An <code>idle</code> time that does not increase does not indicate that the worker is 13401cb0ef41Sopenharmony_cistuck in bootstrap. The following examples shows how the worker's entire 13411cb0ef41Sopenharmony_cilifetime never accumulates any <code>idle</code> time, but is still be able to process 13421cb0ef41Sopenharmony_cimessages.</p> 13431cb0ef41Sopenharmony_ci<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>); 13441cb0ef41Sopenharmony_ci 13451cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (isMainThread) { 13461cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename); 13471cb0ef41Sopenharmony_ci <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> { 13481cb0ef41Sopenharmony_ci worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'hi'</span>); 13491cb0ef41Sopenharmony_ci <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>()); 13501cb0ef41Sopenharmony_ci }, <span class="hljs-number">100</span>).<span class="hljs-title function_">unref</span>(); 13511cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span>; 13521cb0ef41Sopenharmony_ci} 13531cb0ef41Sopenharmony_ci 13541cb0ef41Sopenharmony_ciparentPort.<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>(); 13551cb0ef41Sopenharmony_ci(<span class="hljs-keyword">function</span> <span class="hljs-title function_">r</span>(<span class="hljs-params">n</span>) { 13561cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (--n < <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span>; 13571cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> t = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(); 13581cb0ef41Sopenharmony_ci <span class="hljs-keyword">while</span> (<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>() - t < <span class="hljs-number">300</span>); 13591cb0ef41Sopenharmony_ci <span class="hljs-title function_">setImmediate</span>(r, n); 13601cb0ef41Sopenharmony_ci})(<span class="hljs-number">10</span>);</code> <button class="copy-button">copy</button></pre> 13611cb0ef41Sopenharmony_ci<p>The event loop utilization of a worker is available only after the <a href="#event-online"><code>'online'</code> 13621cb0ef41Sopenharmony_cievent</a> emitted, and if called before this, or after the <a href="#event-exit"><code>'exit'</code> 13631cb0ef41Sopenharmony_cievent</a>, then all properties have the value of <code>0</code>.</p> 13641cb0ef41Sopenharmony_ci<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> 13651cb0ef41Sopenharmony_ci<div class="api_metadata"> 13661cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 13671cb0ef41Sopenharmony_ci</div> 13681cb0ef41Sopenharmony_ci<ul> 13691cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 13701cb0ef41Sopenharmony_ci<li><code>transferList</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object[]></a></li> 13711cb0ef41Sopenharmony_ci</ul> 13721cb0ef41Sopenharmony_ci<p>Send a message to the worker that is received via 13731cb0ef41Sopenharmony_ci<a href="#event-message"><code>require('node:worker_threads').parentPort.on('message')</code></a>. 13741cb0ef41Sopenharmony_ciSee <a href="#portpostmessagevalue-transferlist"><code>port.postMessage()</code></a> for more details.</p> 13751cb0ef41Sopenharmony_ci<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> 13761cb0ef41Sopenharmony_ci<div class="api_metadata"> 13771cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 13781cb0ef41Sopenharmony_ci</div> 13791cb0ef41Sopenharmony_ci<p>Opposite of <code>unref()</code>, calling <code>ref()</code> on a previously <code>unref()</code>ed worker does 13801cb0ef41Sopenharmony_ci<em>not</em> let the program exit if it's the only active handle left (the default 13811cb0ef41Sopenharmony_cibehavior). If the worker is <code>ref()</code>ed, calling <code>ref()</code> again has 13821cb0ef41Sopenharmony_cino effect.</p> 13831cb0ef41Sopenharmony_ci<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> 13841cb0ef41Sopenharmony_ci<div class="api_metadata"> 13851cb0ef41Sopenharmony_ci<span>Added in: v13.2.0, v12.16.0</span> 13861cb0ef41Sopenharmony_ci</div> 13871cb0ef41Sopenharmony_ci<ul> 13881cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 13891cb0ef41Sopenharmony_ci<ul> 13901cb0ef41Sopenharmony_ci<li><code>maxYoungGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 13911cb0ef41Sopenharmony_ci<li><code>maxOldGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 13921cb0ef41Sopenharmony_ci<li><code>codeRangeSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 13931cb0ef41Sopenharmony_ci<li><code>stackSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 13941cb0ef41Sopenharmony_ci</ul> 13951cb0ef41Sopenharmony_ci</li> 13961cb0ef41Sopenharmony_ci</ul> 13971cb0ef41Sopenharmony_ci<p>Provides the set of JS engine resource constraints for this Worker thread. 13981cb0ef41Sopenharmony_ciIf the <code>resourceLimits</code> option was passed to the <a href="#class-worker"><code>Worker</code></a> constructor, 13991cb0ef41Sopenharmony_cithis matches its values.</p> 14001cb0ef41Sopenharmony_ci<p>If the worker has stopped, the return value is an empty object.</p> 14011cb0ef41Sopenharmony_ci<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> 14021cb0ef41Sopenharmony_ci<div class="api_metadata"> 14031cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 14041cb0ef41Sopenharmony_ci</div> 14051cb0ef41Sopenharmony_ci<ul> 14061cb0ef41Sopenharmony_ci<li><a href="stream.html#class-streamreadable" class="type"><stream.Readable></a></li> 14071cb0ef41Sopenharmony_ci</ul> 14081cb0ef41Sopenharmony_ci<p>This is a readable stream which contains data written to <a href="process.html#processstderr"><code>process.stderr</code></a> 14091cb0ef41Sopenharmony_ciinside the worker thread. If <code>stderr: true</code> was not passed to the 14101cb0ef41Sopenharmony_ci<a href="#class-worker"><code>Worker</code></a> constructor, then data is piped to the parent thread's 14111cb0ef41Sopenharmony_ci<a href="process.html#processstderr"><code>process.stderr</code></a> stream.</p> 14121cb0ef41Sopenharmony_ci<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> 14131cb0ef41Sopenharmony_ci<div class="api_metadata"> 14141cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 14151cb0ef41Sopenharmony_ci</div> 14161cb0ef41Sopenharmony_ci<ul> 14171cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> | <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> 14181cb0ef41Sopenharmony_ci</ul> 14191cb0ef41Sopenharmony_ci<p>If <code>stdin: true</code> was passed to the <a href="#class-worker"><code>Worker</code></a> constructor, this is a 14201cb0ef41Sopenharmony_ciwritable stream. The data written to this stream will be made available in 14211cb0ef41Sopenharmony_cithe worker thread as <a href="process.html#processstdin"><code>process.stdin</code></a>.</p> 14221cb0ef41Sopenharmony_ci<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> 14231cb0ef41Sopenharmony_ci<div class="api_metadata"> 14241cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 14251cb0ef41Sopenharmony_ci</div> 14261cb0ef41Sopenharmony_ci<ul> 14271cb0ef41Sopenharmony_ci<li><a href="stream.html#class-streamreadable" class="type"><stream.Readable></a></li> 14281cb0ef41Sopenharmony_ci</ul> 14291cb0ef41Sopenharmony_ci<p>This is a readable stream which contains data written to <a href="process.html#processstdout"><code>process.stdout</code></a> 14301cb0ef41Sopenharmony_ciinside the worker thread. If <code>stdout: true</code> was not passed to the 14311cb0ef41Sopenharmony_ci<a href="#class-worker"><code>Worker</code></a> constructor, then data is piped to the parent thread's 14321cb0ef41Sopenharmony_ci<a href="process.html#processstdout"><code>process.stdout</code></a> stream.</p> 14331cb0ef41Sopenharmony_ci<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> 14341cb0ef41Sopenharmony_ci<div class="api_metadata"> 14351cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 14361cb0ef41Sopenharmony_ci<table> 14371cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 14381cb0ef41Sopenharmony_ci<tr><td>v12.5.0</td> 14391cb0ef41Sopenharmony_ci<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> 14401cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td> 14411cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.5.0</span></p></td></tr> 14421cb0ef41Sopenharmony_ci</tbody></table> 14431cb0ef41Sopenharmony_ci</details> 14441cb0ef41Sopenharmony_ci</div> 14451cb0ef41Sopenharmony_ci<ul> 14461cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 14471cb0ef41Sopenharmony_ci</ul> 14481cb0ef41Sopenharmony_ci<p>Stop all JavaScript execution in the worker thread as soon as possible. 14491cb0ef41Sopenharmony_ciReturns a Promise for the exit code that is fulfilled when the 14501cb0ef41Sopenharmony_ci<a href="#event-exit"><code>'exit'</code> event</a> is emitted.</p> 14511cb0ef41Sopenharmony_ci<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> 14521cb0ef41Sopenharmony_ci<div class="api_metadata"> 14531cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 14541cb0ef41Sopenharmony_ci</div> 14551cb0ef41Sopenharmony_ci<ul> 14561cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 14571cb0ef41Sopenharmony_ci</ul> 14581cb0ef41Sopenharmony_ci<p>An integer identifier for the referenced thread. Inside the worker thread, 14591cb0ef41Sopenharmony_ciit is available as <a href="#workerthreadid"><code>require('node:worker_threads').threadId</code></a>. 14601cb0ef41Sopenharmony_ciThis value is unique for each <code>Worker</code> instance inside a single process.</p> 14611cb0ef41Sopenharmony_ci<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> 14621cb0ef41Sopenharmony_ci<div class="api_metadata"> 14631cb0ef41Sopenharmony_ci<span>Added in: v10.5.0</span> 14641cb0ef41Sopenharmony_ci</div> 14651cb0ef41Sopenharmony_ci<p>Calling <code>unref()</code> on a worker allows the thread to exit if this is the only 14661cb0ef41Sopenharmony_ciactive handle in the event system. If the worker is already <code>unref()</code>ed calling 14671cb0ef41Sopenharmony_ci<code>unref()</code> again has no effect.</p> 14681cb0ef41Sopenharmony_ci</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> 14691cb0ef41Sopenharmony_ci<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> 14701cb0ef41Sopenharmony_ci<p><code>Worker</code>s utilize message passing via <a href="worker_threads.html#class-messageport" class="type"><MessagePort></a> to implement interactions 14711cb0ef41Sopenharmony_ciwith <code>stdio</code>. This means that <code>stdio</code> output originating from a <code>Worker</code> can 14721cb0ef41Sopenharmony_ciget blocked by synchronous code on the receiving end that is blocking the 14731cb0ef41Sopenharmony_ciNode.js event loop.</p> 14741cb0ef41Sopenharmony_ci 14751cb0ef41Sopenharmony_ci<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> { 14761cb0ef41Sopenharmony_ci <span class="hljs-title class_">Worker</span>, 14771cb0ef41Sopenharmony_ci isMainThread, 14781cb0ef41Sopenharmony_ci} <span class="hljs-keyword">from</span> <span class="hljs-string">'worker_threads'</span>; 14791cb0ef41Sopenharmony_ci 14801cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (isMainThread) { 14811cb0ef41Sopenharmony_ci <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>)); 14821cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-number">0</span>; n < <span class="hljs-number">1e10</span>; n++) { 14831cb0ef41Sopenharmony_ci <span class="hljs-comment">// Looping to simulate work.</span> 14841cb0ef41Sopenharmony_ci } 14851cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> { 14861cb0ef41Sopenharmony_ci <span class="hljs-comment">// This output will be blocked by the for loop in the main thread.</span> 14871cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'foo'</span>); 14881cb0ef41Sopenharmony_ci}</code><code class="language-js cjs"><span class="hljs-meta">'use strict'</span>; 14891cb0ef41Sopenharmony_ci 14901cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 14911cb0ef41Sopenharmony_ci <span class="hljs-title class_">Worker</span>, 14921cb0ef41Sopenharmony_ci isMainThread, 14931cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:worker_threads'</span>); 14941cb0ef41Sopenharmony_ci 14951cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (isMainThread) { 14961cb0ef41Sopenharmony_ci <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename); 14971cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-number">0</span>; n < <span class="hljs-number">1e10</span>; n++) { 14981cb0ef41Sopenharmony_ci <span class="hljs-comment">// Looping to simulate work.</span> 14991cb0ef41Sopenharmony_ci } 15001cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> { 15011cb0ef41Sopenharmony_ci <span class="hljs-comment">// This output will be blocked by the for loop in the main thread.</span> 15021cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'foo'</span>); 15031cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre> 15041cb0ef41Sopenharmony_ci<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> 15051cb0ef41Sopenharmony_ci<p>Take care when launching worker threads from preload scripts (scripts loaded 15061cb0ef41Sopenharmony_ciand run using the <code>-r</code> command line flag). Unless the <code>execArgv</code> option is 15071cb0ef41Sopenharmony_ciexplicitly set, new Worker threads automatically inherit the command line flags 15081cb0ef41Sopenharmony_cifrom the running process and will preload the same preload scripts as the main 15091cb0ef41Sopenharmony_cithread. If the preload script unconditionally launches a worker thread, every 15101cb0ef41Sopenharmony_cithread spawned will spawn another until the application crashes.</p></section> 15111cb0ef41Sopenharmony_ci <!-- API END --> 15121cb0ef41Sopenharmony_ci </div> 15131cb0ef41Sopenharmony_ci </div> 15141cb0ef41Sopenharmony_ci </div> 15151cb0ef41Sopenharmony_ci</body> 15161cb0ef41Sopenharmony_ci</html> 1517