<?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: FUJI Goro</title>
    <description>The latest articles on DEV Community by FUJI Goro (@gfx).</description>
    <link>https://dev.to/gfx</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%2F41349%2F104c88b4-cfbb-4e0e-ab30-53f1c12e904a.png</url>
      <title>DEV Community: FUJI Goro</title>
      <link>https://dev.to/gfx</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gfx"/>
    <language>en</language>
    <item>
      <title>Using WebAssembly for a NodeJS Native Addon</title>
      <dc:creator>FUJI Goro</dc:creator>
      <pubDate>Fri, 17 Nov 2017 05:36:28 +0000</pubDate>
      <link>https://dev.to/gfx/using-webassembly-for-a-nodejs-native-addon-dpf</link>
      <guid>https://dev.to/gfx/using-webassembly-for-a-nodejs-native-addon-dpf</guid>
      <description>&lt;p&gt;Recently I have released an npm &lt;a href="https://www.npmjs.com/package/@gfx/zopfli"&gt;@gfx/zopfli&lt;/a&gt;, which is a JavaScript binding to &lt;a href="https://github.com/google/zopfli"&gt;google/zopfli&lt;/a&gt;, as a drop-in replacement to &lt;a href="https://www.npmjs.com/package/node-zopfli"&gt;node-zopfli&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I have a story for this package: it is built with Emscripten into WebAssembly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/gfx/universal-zopfli-js"&gt;https://github.com/gfx/universal-zopfli-js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;WebAssembly is a portable executable format for the web browsers, but it is not only for the web but usable for nodejs native addons. That is, the package includes pre-built executable binary in wasm. No compiler is required to install it. No node-pre-gyp magic is required (in fact node-zopfli has problems with node-pre-gyp &lt;a href="https://github.com/pierreinglebert/node-zopfli/issues/87"&gt;pierreinglebert/node-zopfli#87&lt;/a&gt;). Even Windows users can use it just by &lt;code&gt;npm install @gfx/zofpli&lt;/code&gt; (or &lt;code&gt;yarnpkg add @gfx/zopfli&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;You might be worried about wasm performance, but I think it is already production-ready, although it is slower than native code.&lt;/p&gt;

&lt;p&gt;Here is a benchmark result in the repo on NodejS 8.9.1 (see &lt;a href="https://github.com/gfx/universal-zopfli-js/blob/master/benchmark/random-bytes.js"&gt;benchmark/random-bytes.js&lt;/a&gt; for the code):&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  payload size: 1
&lt;/h4&gt;

&lt;p&gt;universal-zopfli x 106 ops/sec ±0.79% (80 runs sampled)&lt;br&gt;
node-zopfli x 201 ops/sec ±1.99% (82 runs sampled)&lt;/p&gt;
&lt;h4&gt;
  
  
  payload size: 1024
&lt;/h4&gt;

&lt;p&gt;universal-zopfli x 1.37 ops/sec ±12.99% (11 runs sampled)&lt;br&gt;
node-zopfli x 4.62 ops/sec ±3.34% (27 runs sampled)&lt;/p&gt;
&lt;h4&gt;
  
  
  payload size: 1038336
&lt;/h4&gt;

&lt;p&gt;universal-zopfli x 0.26 ops/sec ±6.91% (6 runs sampled)&lt;br&gt;
node-zopfli x 0.39 ops/sec ±1.35% (6 runs sampled)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The speed (ops/sec) of universal-zopfli (i.e. in wasm) is about 30% - 70% of node-zopfli (i.e. in native code). It is good enough for production if you use &lt;code&gt;@gfx/zopfli&lt;/code&gt; as a Zlib alternative in &lt;a href="https://github.com/webpack-contrib/compression-webpack-plugin"&gt;compression-webpack-plugin&lt;/a&gt;. And fortunately, the current state of WebAssembly implementation in NodeJS/V8 is just an MVP! It should be getting better in performance.&lt;/p&gt;

&lt;p&gt;Development of nodejs native addon with Emscripten is difficult for now, but I believe most of NodeJS native addons which do not use systemcalls, including nodejs standard libraries such as zlib, can be replaced with wasm because of its portability.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webassembly</category>
      <category>node</category>
      <category>zopfli</category>
    </item>
  </channel>
</rss>
