<?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: Antoni Kępiński</title>
    <description>The latest articles on DEV Community by Antoni Kępiński (@xxczaki).</description>
    <link>https://dev.to/xxczaki</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%2F102189%2Fbd901565-bb04-42ee-8273-184c1901b200.jpg</url>
      <title>DEV Community: Antoni Kępiński</title>
      <link>https://dev.to/xxczaki</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xxczaki"/>
    <language>en</language>
    <item>
      <title>How to embed a Changelog Podcast Player into a React app?</title>
      <dc:creator>Antoni Kępiński</dc:creator>
      <pubDate>Thu, 16 Jul 2020 14:43:41 +0000</pubDate>
      <link>https://dev.to/xxczaki/how-to-embed-a-changelog-podcast-player-into-a-react-app-g0o</link>
      <guid>https://dev.to/xxczaki/how-to-embed-a-changelog-podcast-player-into-a-react-app-g0o</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I recently had a chance to do a small redesign of my &lt;a href="https://kepinski.me" rel="noopener noreferrer"&gt;personal site&lt;/a&gt;. One of the goals, besides making everything look cleaner, was to embed &lt;a href="https://changelog.com/" rel="noopener noreferrer"&gt;Changelog's&lt;/a&gt; Podcast Player, so that it would play &lt;a href="https://changelog.com/jsparty/85" rel="noopener noreferrer"&gt;85th episode of JS Party&lt;/a&gt;, in which I talked about one of my projects.&lt;/p&gt;

&lt;p&gt;It seems pretty straight forward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;audio&lt;/span&gt; &lt;span class="na"&gt;data-theme=&lt;/span&gt;&lt;span class="s"&gt;"night"&lt;/span&gt; &lt;span class="na"&gt;data-src=&lt;/span&gt;&lt;span class="s"&gt;"https://changelog.com/jsparty/85/embed"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.changelog.com/uploads/jsparty/85/js-party-85.mp3"&lt;/span&gt; &lt;span class="na"&gt;preload=&lt;/span&gt;&lt;span class="s"&gt;"none"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"changelog-episode"&lt;/span&gt; &lt;span class="na"&gt;controls&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/audio&amp;gt;&amp;lt;p&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://changelog.com/jsparty/85"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;JS Party 85: Building PizzaQL at the age of 16&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt; – Listen on &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://changelog.com/"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Changelog.com&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;async&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"//cdn.changelog.com/embed.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And works just fine on a static HTML site. However, in my case, I was dealing with a React application, powered by &lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;Next.js&lt;/a&gt; with Static Site Generation (SSG). I also focused much on performance and accessibility.&lt;/p&gt;

&lt;p&gt;That's exactly why I decided to make this tutorial!&lt;/p&gt;

&lt;h2&gt;
  
  
  Tutorial
&lt;/h2&gt;

&lt;p&gt;First of all, we need to embed an &lt;code&gt;audio&lt;/code&gt; tag into our app. It's fairly easy:&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;audio&lt;/span&gt;
    &lt;span class="nx"&gt;controls&lt;/span&gt;
    &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;night&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://changelog.com/jsparty/85/embed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://cdn.changelog.com/uploads/jsparty/85/js-party-85.mp3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nx"&gt;preload&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;none&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;changelog-episode&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Make sure to replace &lt;code&gt;data-src&lt;/code&gt; and &lt;code&gt;src&lt;/code&gt; attributes so that they link to an episode, which you want to embed. &lt;strong&gt;It does not need to be the JS Party podcast&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;We now need to load a script, which will add an &lt;code&gt;iframe&lt;/code&gt; with the player itself. Normally, we would obtain it from &lt;a href="https://cdn.changelog.com/embed.js" rel="noopener noreferrer"&gt;https://cdn.changelog.com/embed.js&lt;/a&gt;, but instead, we are going to self-host it. Here are the reasons why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Self-hosting assets (like fonts or scripts) is significantly faster;&lt;/li&gt;
&lt;li&gt;The default Changelog's script does not add a &lt;code&gt;title&lt;/code&gt; attribute to the created &lt;code&gt;iframe&lt;/code&gt; - this will lower our site accessibility score on tools like &lt;a href="https://developers.google.com/web/tools/lighthouse" rel="noopener noreferrer"&gt;Lighthouse&lt;/a&gt;;&lt;/li&gt;
&lt;li&gt;The default Changelog's script sometimes throws &lt;code&gt;Uncaught TypeError: Cannot read property 'postMessage' of null.&lt;/code&gt;.*&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*This does not break the functionality of the player and therefore we can easily fix it using a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch" rel="noopener noreferrer"&gt;try...catch&lt;/a&gt; statement.&lt;/p&gt;

&lt;p&gt;In order to edit the embed script, we first need to unminify the original one from Changelog's CDN. We can then modify it like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gs"&gt;!(function (e) {
&lt;/span&gt;    function t(t) {
        var r = t.getAttribute("data-src"),
            i = t.getAttribute("data-theme") || "night",
            n = e.createElement("iframe");
        n.setAttribute("src", r + "?theme=" + i + "&amp;amp;referrer=" + e.location.href),
            n.setAttribute("width", "100%"),
            n.setAttribute("height", "220"),
            n.setAttribute("scrolling", "no"),
            n.setAttribute("frameborder", "no"),
&lt;span class="gi"&gt;+           n.setAttribute("title", "Podcast"),
&lt;/span&gt;            t.parentNode.replaceChild(n, t),
            (this.id = +new Date()),
            (this.src = n.src),
            (this.iframe = n);
    }
    var r = "https://changelog.com",
        i = e.getElementsByClassName("changelog-episode"),
        n = [],
        a = function (e, t) {
&lt;span class="gd"&gt;-            (t.context = "player.js"), (t.version = "0.0.11"), (t.listener = e.id), e.iframe.contentWindow.postMessage(JSON.stringify(t), r);
&lt;/span&gt;&lt;span class="gi"&gt;+            try {
+                (t.context = "player.js"), (t.version = +"0.0.11"), (t.listener = e.id), +e.iframe.contentWindow.postMessage(JSON.stringify(t), r);
+            } catch {}
&lt;/span&gt;        };
    window.addEventListener("message", function (e) {
        if (e.origin !== r) return !1;
        var t = JSON.parse(e.data);
        if ("player.js" !== t.context) return !1;
        if ("ready" === t.event) for (var i = n.length - 1; i &amp;gt;= 0; i--) n[i].src === t.value.src &amp;amp;&amp;amp; a(n[i], { method: "addEventListener", value: "play" });
        if ("play" === t.event) for (i = n.length - 1; i &amp;gt;= 0; i--) n[i].id !== t.listener &amp;amp;&amp;amp; a(n[i], { method: "pause" });
    });
    for (var s = i.length - 1; s &amp;gt; -1; s--) n.push(new t(i[s]));
})(document);
&lt;span class="err"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;We are going to load the script in a &lt;code&gt;useEffect&lt;/code&gt; hook so that it won't run on the server (where a browser API, like &lt;code&gt;document&lt;/code&gt; and &lt;code&gt;window&lt;/code&gt;, isn't available):&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="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;script&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;script&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/embed.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Since I'm using Next.js I can place the file in the `public` folder and refer to it like so.&lt;/span&gt;
    &lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Make sure the script isn't render-blocking.&lt;/span&gt;
    &lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;defer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;script&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! You now have a beautiful player on your site.&lt;/p&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;p&gt;The approach of using the &lt;code&gt;useEffect&lt;/code&gt; hook was taken from &lt;a href="https://github.com/nodegui/react-nodegui/blob/8465d3de6381651bfa6a88a82d3cf8acf3681bff/website/src/components/Talks.js#L15-L20" rel="noopener noreferrer"&gt;React NodeGui website source code&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Thank you for reading my post! I hope you will find it useful ;)&lt;/p&gt;

</description>
      <category>react</category>
      <category>ssr</category>
      <category>tutorial</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>How to make your Electron app faster 🚀⚡</title>
      <dc:creator>Antoni Kępiński</dc:creator>
      <pubDate>Sun, 11 Aug 2019 11:56:50 +0000</pubDate>
      <link>https://dev.to/xxczaki/how-to-make-your-electron-app-faster-4ifb</link>
      <guid>https://dev.to/xxczaki/how-to-make-your-electron-app-faster-4ifb</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;So you just made a nice &amp;amp; shiny new app with Electron. It works as expected, has a beautiful UI, &lt;del&gt;eats a lot of RAM&lt;/del&gt; and &lt;del&gt;is slow&lt;/del&gt;, right?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you don't know that already, &lt;a href="https://electronjs.org/" rel="noopener noreferrer"&gt;Electron&lt;/a&gt; is an open-source framework for building cross-platform desktop applications using HTML, CSS &amp;amp; (duh) JavaScript. It was initially called Atom Shell and was developed by Github to power their text editor - &lt;a href="https://atom.io/" rel="noopener noreferrer"&gt;Atom&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Everything sound great, right? You don't need to learn another language to create cross-platform apps and instead utilize your existing knowledge.&lt;/p&gt;

&lt;p&gt;Unfortunately, Electron has some downsides. To render your application UI, it uses Chromium, which is always bundled in your final application. Because of that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Size of application built with Electron is typically around ~120 MB.&lt;/li&gt;
&lt;li&gt;Electron apps usually use a lot of RAM.&lt;/li&gt;
&lt;li&gt;UI might feel a bit slow, especially on the older computers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, even though we can't just delete the whole Chromium from our app and reduce it's size, we can still do something about the RAM usage &amp;amp; speed.&lt;/p&gt;

&lt;p&gt;That's why I would like to show you 4 tips on how to make your Electron app faster!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Use V8 Engine code cache
&lt;/h2&gt;

&lt;p&gt;Chrome V8 is basically a JavaScript engine, that powers both Node.js &amp;amp; Chromium browser. One of it's feature is the &lt;a href="https://v8.dev/blog/code-caching" rel="noopener noreferrer"&gt;code caching&lt;/a&gt;, which can speed up instantiation time of you app.&lt;/p&gt;

&lt;p&gt;To make sure this feature is enabled, we will use a package called &lt;a href="https://github.com/zertosh/v8-compile-cache" rel="noopener noreferrer"&gt;&lt;code&gt;v8-compile-cache&lt;/code&gt;&lt;/a&gt;, created by &lt;a href="https://github.com/zertosh" rel="noopener noreferrer"&gt;Andres Suarez&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Install the package&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;v8-compile-cache
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then, in your entry module add the following code:&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="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;v8-compile-cache&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// or, using ES6 `import`:&lt;/span&gt;
&lt;span class="c1"&gt;// import 'v8-compile-cache';&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: Take a look at the &lt;a href="https://github.com/zertosh/v8-compile-cache#benchmarks" rel="noopener noreferrer"&gt;benchmarks&lt;/a&gt; to see how &lt;code&gt;v8-compile-cache&lt;/code&gt; speeds up popular modules 😄&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  2. Use a module bundler
&lt;/h2&gt;

&lt;p&gt;This suggestion is especially useful in larger projects, that use a good amount of packages. Using a module bundler, like &lt;a href="https://webpack.js.org/" rel="noopener noreferrer"&gt;Webpack&lt;/a&gt; will enable features like &lt;a href="https://webpack.js.org/guides/tree-shaking/" rel="noopener noreferrer"&gt;tree shaking&lt;/a&gt; and &lt;a href="https://webpack.js.org/guides/code-splitting/" rel="noopener noreferrer"&gt;code splitting&lt;/a&gt;, which will make your code smaller &amp;amp; faster.&lt;/p&gt;

&lt;p&gt;You will also be able to use some of the community-made &lt;a href="https://webpack.js.org/plugins/" rel="noopener noreferrer"&gt;plugins&lt;/a&gt; &amp;amp; &lt;a href="https://webpack.js.org/loaders/" rel="noopener noreferrer"&gt;loaders&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Use faster JavaScript methods, especially when working with DOM
&lt;/h2&gt;

&lt;p&gt;Look at the following 2 lines of code:&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;elementOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;one&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;elementTwo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#one&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They both do the same thing - find the DOM element. But take a look at the  &lt;a href="https://jsperf.com/getelementbyid-vs-queryselector/11" rel="noopener noreferrer"&gt;performance benchmark&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3otg8mx7ljbot0syyx3n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3otg8mx7ljbot0syyx3n.png" width="800" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the first method is more than 2 times faster, than the second one. And yes - that does not mean, that the second method is slow - both are really fast in practical use cases, but when we use a lot of slower methods in our application, replacing them with faster alternatives can really make a difference!&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Use WebAssembly (or native addons)
&lt;/h2&gt;

&lt;p&gt;This change can really speed up you application, but it also requires the most amount of work.&lt;/p&gt;

&lt;p&gt;If your application has to, for example, calculate on a very big numbers really fast or inspect large amounts of data, JavaScript might be too slow 😢&lt;/p&gt;

&lt;p&gt;That's exactly when &lt;a href="https://webassembly.org/" rel="noopener noreferrer"&gt;WebAssembly&lt;/a&gt; and &lt;a href="https://nodejs.org/api/addons.html" rel="noopener noreferrer"&gt;native addons&lt;/a&gt; comes in handy!&lt;/p&gt;

&lt;p&gt;Take a look at my Electron application - &lt;a href="https://github.com/elcalc/elcalc" rel="noopener noreferrer"&gt;elcalc&lt;/a&gt;, a calculator. To do more advanced math, I used a really nice library called &lt;a href="https://mathjs.org/" rel="noopener noreferrer"&gt;math.js&lt;/a&gt;. Unfortunately, when I was testing advanced &amp;amp; complex calculations, there was a noticeable timeout between clicking the evaluate button and the result showing up. Not good...&lt;/p&gt;

&lt;p&gt;I decided to write a simple Rust code, that will handle the math &amp;amp; convert it to WASM (shorthand for WebAssembly). I used a crate (something like npm package, but for Rust) called &lt;a href="https://crates.io/crates/meval" rel="noopener noreferrer"&gt;&lt;code&gt;meval&lt;/code&gt;&lt;/a&gt;, that parses math expressions and evaluated them.&lt;/p&gt;

&lt;p&gt;To actually generate the WASM, I used &lt;a href="https://rustwasm.github.io/wasm-pack/" rel="noopener noreferrer"&gt;wasm-pack&lt;/a&gt; and it's &lt;a href="https://github.com/rustwasm/rust-webpack-template/tree/master/template" rel="noopener noreferrer"&gt;rust-webpack-template&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I also used a Webpack plugin, called &lt;a href="https://github.com/jlegrone/optimize-wasm-webpack-plugin" rel="noopener noreferrer"&gt;optimize-wasm-webpack-plugin &lt;br&gt;
&lt;/a&gt;, that (as it's name says) optimized WebAssembly files using &lt;a href="https://github.com/WebAssembly/binaryen" rel="noopener noreferrer"&gt;binaryen&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And to lazy-load the WASM function in my JavaScript code, I used the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports" rel="noopener noreferrer"&gt;dynamic import&lt;/a&gt; proposal:&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="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../crate/pkg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do something&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now my calculator evaluates math expressions much faster 🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  BONUS: 5. If you care about app size, use something else
&lt;/h2&gt;

&lt;p&gt;If you care much about your app size, there is an alternative to Electron, called &lt;a href="https://github.com/GoogleChromeLabs/carlo" rel="noopener noreferrer"&gt;Carlo&lt;/a&gt;. Instead of including Chromium in your app bundle, it uses the locally installed Google Chrome browser on user's computer. &lt;/p&gt;

&lt;p&gt;Unfortunately this means that when user does not have Google Chrome installed, your app won't launch and it will display an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;p&gt;Some suggestions listed in this post were taken from &lt;br&gt;
&lt;a href="https://medium.com/@felixrieseberg" rel="noopener noreferrer"&gt;Felix Rieseberg's&lt;/a&gt; Medium article, called &lt;a href="https://medium.com/@felixrieseberg/javascript-on-the-desktop-fast-and-slow-2b744dfb8b55" rel="noopener noreferrer"&gt;"JavaScript on the Desktop, Fast and Slow"&lt;/a&gt;. I highly recommend checking it out!&lt;/p&gt;

&lt;p&gt;Thank you for reading my post! I hope you will find it useful ;)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>electron</category>
      <category>tutorial</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
