<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Christophe Marois</title>
    <description>The latest articles on DEV Community by Christophe Marois (@christophemarois).</description>
    <link>https://dev.to/christophemarois</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F320875%2F6ea02179-2876-4259-956b-035a88716ab2.jpeg</url>
      <title>DEV Community: Christophe Marois</title>
      <link>https://dev.to/christophemarois</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/christophemarois"/>
    <language>en</language>
    <item>
      <title>Seamless workers for Node.js</title>
      <dc:creator>Christophe Marois</dc:creator>
      <pubDate>Mon, 20 Jan 2020 17:40:15 +0000</pubDate>
      <link>https://dev.to/christophemarois/seamless-workers-for-node-js-213</link>
      <guid>https://dev.to/christophemarois/seamless-workers-for-node-js-213</guid>
      <description>&lt;p&gt;Node.js supports native &lt;a href="http://nodejs.org/api/worker_threads.html"&gt;Worker Threads&lt;/a&gt; without flags since 12.0, and this finally allows us to circumvent the limitations associated with the single-threaded nature of JavaScript.&lt;/p&gt;

&lt;p&gt;Workers in Node can only communicate via a &lt;a href="https://en.wikipedia.org/wiki/Remote_procedure_call"&gt;RPC&lt;/a&gt; pattern (same as their web counterparts), which is not a bad thing at all: it enables concurrency, like the &lt;code&gt;go&lt;/code&gt; coroutine pattern used in golang.&lt;/p&gt;

&lt;p&gt;However, sometimes you just want a drop-in way to execute a function without blocking the main thread (for instance, regenerating a server's cached data without blocking incoming requests to the previous cached data).&lt;/p&gt;

&lt;p&gt;For WebWorkers, &lt;a href="https://github.com/developit/workerize"&gt;Workerize&lt;/a&gt; has been providing exactly this. You can execute a function normally without having to change its signature, but it runs in a worker under-the-hood.&lt;/p&gt;

&lt;p&gt;I created the &lt;code&gt;node-inline-worker&lt;/code&gt; npm package (&lt;a href="https://github.com/christophemarois/node-inline-worker"&gt;Github&lt;/a&gt;) to achieve the same thing, leveraging the Worker Thread of Node.js instead of the WebWorker API, while keeping the same interface API as Workerize. With it, making a function truly non-blocking is as simple as changing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;expensiveFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node-inline-worker&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;expensiveFn&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note: Worker Threads will not magically solve your performance issues, and have some downfalls. For instance, Node.js documentation's states that:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Workers (threads) are useful for performing CPU-intensive JavaScript operations. They will not help much with I/O-intensive work. Node.js’s built-in asynchronous I/O operations are more efficient than Workers can be.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I suggest that you read the &lt;a href="https://nodejs.org/api/worker_threads.html"&gt;Worker Thread&lt;/a&gt; documentation page (especially the &lt;em&gt;Notable differences inside a Worker environment&lt;/em&gt; section) to find out if workers are suitable for your use case.&lt;/p&gt;

&lt;p&gt;For more information on how to use &lt;code&gt;node-inline-worker&lt;/code&gt;, go read the very straightforward &lt;a href="https://github.com/christophemarois/node-inline-worker#usage"&gt;example&lt;/a&gt; on the Github repo's page.&lt;/p&gt;

&lt;p&gt;Have a good day!&lt;/p&gt;

</description>
      <category>node</category>
      <category>worker</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
