How to make your Electron app faster 🚀⚡
Tags: javascript, electron, tutorial, performance
Published: 2019-08-11 11:56:50

> So you just made a nice & shiny new app with Electron. It works as expected, has a beautiful UI, ~~eats a lot of RAM~~ and ~~is slow~~, right?

## Introduction

If you don't know that already, [Electron](https://electronjs.org/) is an open-source framework for building cross-platform desktop applications using HTML, CSS & (duh) JavaScript. It was initially called Atom Shell and was developed by Github to power their text editor - [Atom](https://atom.io/).

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

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

* Size of application built with Electron is typically around ~120 MB.
* Electron apps usually use a lot of RAM.
* UI might feel a bit slow, especially on the older computers.

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 & speed.

That's why I would like to show you 4 tips on how to make your Electron app faster!

## 1. Use V8 Engine code cache

Chrome V8 is basically a JavaScript engine, that powers both Node.js & Chromium browser. One of it's feature is the [code caching](https://v8.dev/blog/code-caching), which can speed up instantiation time of you app.

To make sure this feature is enabled, we will use a package called [`v8-compile-cache`](https://github.com/zertosh/v8-compile-cache), created by [Andres Suarez](https://github.com/zertosh):

```bash
# Install the package
$ npm install v8-compile-cache
```

and then, in your entry module add the following code:

```js
require('v8-compile-cache');

// or, using ES6 `import`:
// import 'v8-compile-cache';
```

> Note: Take a look at the [benchmarks](https://github.com/zertosh/v8-compile-cache#benchmarks) to see how `v8-compile-cache` speeds up popular modules :smile:

## 2. Use a module bundler

This suggestion is especially useful in larger projects, that use a good amount of packages. Using a module bundler, like [Webpack](https://webpack.js.org/) will enable features like [tree shaking](https://webpack.js.org/guides/tree-shaking/) and [code splitting](https://webpack.js.org/guides/code-splitting/), which will make your code smaller & faster.

You will also be able to use some of the community-made [plugins](https://webpack.js.org/plugins/) & [loaders](https://webpack.js.org/loaders/).

## 3. Use faster JavaScript methods, especially when working with DOM

Look at the following 2 lines of code:

```js
const elementOne = document.getElementById('one');
const elementTwo = document.querySelector('#one');
```

They both do the same thing - find the DOM element. But take a look at the  [performance benchmark](https://jsperf.com/getelementbyid-vs-queryselector/11):

![](https://thepracticaldev.s3.amazonaws.com/i/5d04g3gnchv9ivv0648f.png)

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!

## 4. Use WebAssembly (or native addons)

This change can really speed up you application, but it also requires the most amount of work.

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 :cry:

That's exactly when [WebAssembly](https://webassembly.org/) and [native addons](https://nodejs.org/api/addons.html) comes in handy!

Take a look at my Electron application - [elcalc](https://github.com/elcalc/elcalc), a calculator. To do more advanced math, I used a really nice library called [math.js](https://mathjs.org/). Unfortunately, when I was testing advanced & complex calculations, there was a noticeable timeout between clicking the evaluate button and the result showing up. Not good...

I decided to write a simple Rust code, that will handle the math & convert it to WASM (shorthand for WebAssembly). I used a crate (something like npm package, but for Rust) called [`meval`](https://crates.io/crates/meval), that parses math expressions and evaluated them.

To actually generate the WASM, I used [wasm-pack](https://rustwasm.github.io/wasm-pack/) and it's [rust-webpack-template](https://github.com/rustwasm/rust-webpack-template/tree/master/template).

I also used a Webpack plugin, called [optimize-wasm-webpack-plugin 
](https://github.com/jlegrone/optimize-wasm-webpack-plugin), that (as it's name says) optimized WebAssembly files using [binaryen](https://github.com/WebAssembly/binaryen).

And to lazy-load the WASM function in my JavaScript code, I used the [dynamic import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports) proposal:

```js
import('../crate/pkg').then(async module => {
    // do something
});
```

Now my calculator evaluates math expressions much faster :rocket:

## BONUS: 5. If you care about app size, use something else

If you care much about your app size, there is an alternative to Electron, called [Carlo](https://github.com/GoogleChromeLabs/carlo). Instead of including Chromium in your app bundle, it uses the locally installed Google Chrome browser on user's computer. 

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

## Credits

Some suggestions listed in this post were taken from 
[Felix Rieseberg's](https://medium.com/@felixrieseberg) Medium article, called ["JavaScript on the Desktop, Fast and Slow"](https://medium.com/@felixrieseberg/javascript-on-the-desktop-fast-and-slow-2b744dfb8b55). I highly recommend checking it out!


Thank you for reading my post! I hope you will find it useful ;)