<?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: Rohit Lohar</title>
    <description>The latest articles on DEV Community by Rohit Lohar (@rohitlohar45).</description>
    <link>https://dev.to/rohitlohar45</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%2F777949%2F7c3df029-7eee-40b9-807d-c7a60686c974.jpg</url>
      <title>DEV Community: Rohit Lohar</title>
      <link>https://dev.to/rohitlohar45</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rohitlohar45"/>
    <language>en</language>
    <item>
      <title>Node.js and WebAssembly: A Powerful Combination for Server-Side Performance</title>
      <dc:creator>Rohit Lohar</dc:creator>
      <pubDate>Mon, 25 Mar 2024 13:05:43 +0000</pubDate>
      <link>https://dev.to/rohitlohar45/nodejs-and-webassembly-a-powerful-combination-for-server-side-performance-4apf</link>
      <guid>https://dev.to/rohitlohar45/nodejs-and-webassembly-a-powerful-combination-for-server-side-performance-4apf</guid>
      <description>&lt;p&gt;Node.js, renowned for its proficiency in handling asynchronous I/O and event-driven architectures, is widely favored for building fast and scalable server-side applications. However, when confronted with computationally intensive tasks, JavaScript's inherent performance limitations may become apparent.&lt;/p&gt;

&lt;p&gt;Enter WebAssembly (WASM), a low-level assembly-like language designed to act as a compilation target for a multitude of high-level languages, including C/C++, Rust, and AssemblyScript. WebAssembly modules, denoted by the &lt;code&gt;.wasm&lt;/code&gt; extension, offer near-native performance while ensuring portability across various environments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Node.js Embraces WebAssembly&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Acknowledging the potential of WebAssembly, Node.js provides intrinsic support for integrating WASM modules seamlessly into server-side applications. This integration opens avenues for a diverse range of performance-critical applications, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Image and Video Processing&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine Learning Inference&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scientific Computations&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Audio and Signal Processing&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cryptographic Operations&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Concepts and APIs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To harness the power of WebAssembly within Node.js, understanding the following essential concepts and APIs is paramount:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;WebAssembly Module:&lt;/strong&gt; This encapsulates the compiled code from your chosen language in a binary format.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;WebAssembly&lt;/code&gt; Object:&lt;/strong&gt; Node.js offers this built-in object, furnishing methods for loading and manipulating WASM modules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;compile()&lt;/code&gt; Method:&lt;/strong&gt; Employed for asynchronously compiling the WASM module from its binary representation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;instance.exports&lt;/code&gt; Object:&lt;/strong&gt; Exposes functions and variables defined within the WASM module, facilitating their invocation from Node.js.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Interoperability:&lt;/strong&gt; Facilitates seamless interaction between JavaScript objects/functions and those within the WASM module, and vice versa.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example: Node.js Utilizing a Simple WASM Module (C++)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's consider a rudimentary C++ function computing the factorial of a number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Factorial of "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" is: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compile the above code into a WASM module using a C++ compiler supporting WASM targeting (e.g., Emscripten):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;emcc factorial.cpp &lt;span class="nt"&gt;-o&lt;/span&gt; factorial.wasm &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="nv"&gt;WASM&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Node.js Code Leveraging the WASM Module&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&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;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WebAssembly&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&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;wasi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;runFactorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wasmBuffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;factorial.wasm&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;module&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;WebAssembly&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wasmBuffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;instance&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;WebAssembly&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;instantiate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;wasi&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;WebAssembly&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WASI&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;factorialFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&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="nf"&gt;factorialFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Factorial of &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;runFactorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We import the &lt;code&gt;fs&lt;/code&gt; module for file system access and the &lt;code&gt;WebAssembly&lt;/code&gt; object from the &lt;code&gt;wasi&lt;/code&gt; module.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;runFactorial&lt;/code&gt; function asynchronously reads the WASM module, compiles it, and creates an instance.&lt;/li&gt;
&lt;li&gt;We access the exported &lt;code&gt;factorial&lt;/code&gt; function from the instance and call it with the desired number.&lt;/li&gt;
&lt;li&gt;The result is subsequently printed to the console.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Additional Considerations&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;WASI (WebAssembly System Interface):&lt;/strong&gt; While the provided example focuses on Node.js-specific WASI usage, a more standardized WASI specification exists, facilitating access to file systems, networks, and other system resources in a sandboxed manner.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance Gains:&lt;/strong&gt; The performance boost derived from integrating WASM modules hinges on the specific task and code optimization level. Conducting thorough benchmarking is essential to gauge the impact accurately.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Handling:&lt;/strong&gt; Implement robust error handling mechanisms to gracefully manage compilation errors, WASM module loading issues, or unexpected behavior within the WASM code, ensuring a resilient application architecture.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webassembly</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>Demo</title>
      <dc:creator>Rohit Lohar</dc:creator>
      <pubDate>Fri, 22 Mar 2024 10:07:20 +0000</pubDate>
      <link>https://dev.to/rohitlohar45/demo-2fm9</link>
      <guid>https://dev.to/rohitlohar45/demo-2fm9</guid>
      <description>&lt;p&gt;Demo&lt;/p&gt;

</description>
      <category>hello</category>
    </item>
  </channel>
</rss>
