Opinion: Electron Is Flash For The Desktop

Image: iStock

All too often, I look at how much of my system's resources Slack is using, even when I haven't given it any action to perform, and it's guzzling memory. What is Slack doing?

Image: Joseph Gentle

The process was in the background when this happened. I wasn't even interacting with it - I was in a meeting. I only noticed because my laptop fans were whurring when I got back. Restarting Slack seemed to fix it for now.

But that's not abnormal for Slack. In fact, Slack often idles at 5% CPU usage. Whats it doing? I have no idea.

And I bet the Slack team doesn't know either. How many lines of code do you think the Slack team wrote to make their client work? I'd guess around 50k. Maybe 100k. But Slack isn't a native app. At least - not a normal native app. Its built on top of electron, so when you download Slack you're actually downloading a complete copy of Google Chrome. Chrome, at the time of writing is 15 million non-comment lines. When you download Slack, 99% of the code is 'below the water'.

And chrome is a hog. Its huge and complicated. It uses ram and CPU like nobody's business, and it totally thrashes your battery life.

You can think of Slack as a small javascript program running inside another operating system VM (chrome), that you have to run in order to essentially chat on IRC. Even if you've got the real chrome open, each electron app runs its own, extra copy of the whole VM.

And its not a stretch to call chrome an OS. By lines of code, chrome is about the same size as the linux kernel. Like the linux kernel it has APIs for all sorts of hardware, including opengl, VR, MIDI. It has an embedded copy of SQLite, memory management and its own task manager. On MacOS it even contains a userland USB driver for xbox360 controllers. (I know its there because I wrote it. Sorry.)

Does Slack contain my code to use xbox controllers? Does the Slack team know? Does anyone know? I mean, the Slack app is 160 megs on disk. Thats about the size of 70 uncompressed copies of Lord Of The Rings. Who knows whats in there? The other electron apps I have on my computer are Spotify (Edit: Not quite - see below) (200 megs) and Atom (260 megs). The first time I installed linux I did it from floppy disks. It would take 450 floppy disks to store these three simple apps. Together these apps are about the size of the standard desktop ubuntu distribution. Which y'know, probably contains an IRC client, a text editor and a music player. An an entire operating system, user space and web browser.

Sure! You say. Disk size is cheap you say! Sure but ram sure isn't. The brand-spanking-new macbook pros only ship with 8 GB ram by default. Because of battery concerns you can't configure them past 16 gigs. And right now Slack is sitting on somewhere between 300 megs to 1 gig of my laptop's ram:

Image: Joesph Gentle

I mean come on. Its a text chat program.

The other thing that isn't plentiful is battery life. The way modern CPUs conserve battery is by turning themselves off anytime they can (when nothing is scheduled). The bane of power management is programs that use a few percent of your CPU constantly. They cause your CPU to constantly wake themselves up, go to sleep and wake again. Thats the perfect way to burn that precious battery life. If anyone has time I'd love to see how much spotify, Slack and atom (just left open not doing anything) decrease the battery life of a modern laptop. Because they're crazy.

Image: Joseph Gentle

And no - spotify isn't playing any music. Its just ... running. Doing mysterious chrome things. Its using a few percent of my CPU too, by the way. Just to exist.

(Vindictively while I've been writing this chrome has jumped up to 100% cpu usage, assigned to the mysterious 'browser' process in chrome's internal task manager. Thanks chrome.)

To be clear, javascript on the desktop isn't the problem. In fact, I think the APIs work with in the modern web are way better than the APIs that exist on desktop. We should use them.

But we need ways to use those nice new paradigms (react and friends) on the desktop without running more blood copies of chrome. I just ... don't care about your app enough to justify running more chrome instances. As a developer its really easy to fall into the trap of assuming your app / website / whatever is a gift to humanity and the most important thing your users are doing. Why not use a few of their excess system resources? But we have to fight that mindset. That path lies a world where we can't have nice things.

That path lies a world where our laptop batteries need to grow ever larger to support our CPUs doing even more dumb crap. That way lies the return of shockwave flash, of warm phones in our pockets which are mysteriously flat when we want to use them. Of getting paranoid about battery life and closing apps the instant we're done with them. (Looking at you, iTunes and Mischief.)

Just Say NO to electron

Developers don't let friends write electron apps. If you want to use JS and react to make a native app, try react native instead. Its like electron, but you don't need to distribute a copy of chrome to all your users, and we don't need to run another copy of chrome to use your app. It turns out modern operating systems already have nice, fast UI libraries. So use them you clod!

The other sad fact is that even most developers have no idea that this is even happening on their computers. They run Slack but have no idea how hungry it is. As a developer its your responsibility to know this stuff. Practice seeing. Learn profiling tools. Get iStatMeters or one of the free equivalents. You can't improve what you don't measure.

Maybe we should be buying slower computers so we feel the pain. Facebook has been internally intentionally slowing down their office internet once a week to help build empathy with their users in other 3rd world internet speed countries (coughAustraliacough). Maybe as developers we should do this with our computers too, or just run our code way slower than normal so we can build an intuition around performance. A few years ago I left my laptop at work over a long weekend. Instead of making a trip out to grab it I decided to hook up my raspberry pi (slow-ass gen 1) and use that as a developer machine.

Suddenly lots of things that were 'instant' on my normal i7 laptop started feeling awfully sluggish. So I spent the weekend fixing them to make my development workflow smooth. All that perf tuning work carries across to our regular machines. Dropping startup time from 5 seconds to 2 seconds on a raspberry pi feels huge. The same improvement became a drop from 0.5 seconds to 0.2 seconds or something. Thats still super noticeable for users. A 0.5 second startup time is small enough that its easy to overlook during development, but dropping it to 0.2 seconds feels obviously faster.

Users: Please complain more about slow programs. Its 2016. We carry supercomputers in our pockets. Its simply not ok for apps to be sluggish.

Developers: Performance matters. Memory usage matters. I don't care if you're the prettiest girl at the dance, Slack. I quit you the moment I walk out of the office. I delete you from my computer when I can. Slow is a bug. The fastest program is the one you don't run. So stop embedding the entirety of chrome in your app.

Also all you web devs: Go learn C or Rust or something. Your program runs on a computer. Until you know how that computer works, you're doomed. And until then get off my lawn shakes fist.

Oh, and read this talk on the website obesity crisis. Its very funny. And very sad. And very true.

Edit: Spotify actually uses the Chromium Embedded Framework directly instead of running via electron. It still embeds chrome though. I didn't know that when I wrote this article, but I stand by what I said above about the resulting performance.

You can read more of Joseph Gentle on his blog and Twitter.



    I'm curious as to why you suggest React Native as an alternative to Electron. Electron only works on the desktop and React Native only works on mobile, it doesn't seem like a proper alternative at all?

        You show a distinct lack of understanding over what electron actually is and why companies would use it. Firstly, Slack already had a web app consisting of thousands of lines of code, so to suggest they should convert the whole lot to react native to create a desktop app is laughable. I imagine they also want to support Windows 7-10 where react native only supports 10.

        If you want to use JS and react to make a native app, try react native instead. Its like electron, but you don't need to distribute a copy of chrome to all your users

        React native is nothing like electron. React native was originally designed for mobile and electron was originally designed for desktop. React has recently brought in experimental support for desktop operating systems and supports no built in interactions with any of the OS features. Sure there are some plugins, but it comes nowhere near close to electron.

        Also all you web devs: Go learn C or Rust or something

        Yeah, great way to create a cross platform app with a single codebase for sensible money.

        Last edited 22/04/17 4:05 am

          He is saying to learn C or Rust because you need to learn the low level details of how a computer works to use these languages for anything useful (personally I think Rust is overrated, you pretty much need to be an expert at C/C++ to get any benefit).

          His argument is still valid for desktop apps if there is no existing codebase, plus if you want to make a desktop app that is performant.

    There's nothing mysterious about the Browser process. It's the process for the master application, all the things that aren't partitioned into their own child processes. Settings management, extension management (but not individual extensions), window and tab management, etc. Subprocesses can access shared memory, and that memory belongs to the main process, so subprocesses can cause the main process's memory usage to blow out if they're being dumb.

    I don't know Electron very well, but I'm do know Chrome/Chromium pretty well and I don't think it's the cause of your issues unless the embedded version is quite old. On Chrome 57 just as an example, I'm sitting here with 46 tabs open across 7 browser windows (one of which is Google Play Music) and 10 extensions including some pretty heavy ones like Privacy Badger (they really need to rewrite this one, much as I love it) and CPU usage fluctuates between 0.5 and 0.8%. At least three tabs I know for sure are maintaining Websockets connections and trigger Javascript updates fairly regularly so that accounts for a lot of the spiking. Total RAM use is 3GB, but half of that is taken up by three tabs in particular (the Gmail client-side JS is a bit notorious for building RAM use over time).

    Power usage estimation isn't a great metric, but one reason that may appear higher is Chrome has managed subprocess memory with a full-fledged garbage collector. Periodically that will run, more frequently if you're close to capping out your overall system memory, and it performs small reads/writes to process memory for all processes. Doing anything to memory is included in power use estimation even if CPU sits flat at rounded 0.0%.

    If your Electron apps are eating a few percent of CPU all the time (or even regularly), they're doing something wrong. Maybe Electron isn't interfacing correctly with Chrome's V8 engine, or maybe it's polling events too frequently. Or maybe the app itself is running JS timers that it doesn't need to.

    Like I said, I don't know Electron's guts so I can't say where the problem would lie, I just doubt it's in the Chrome application itself. The symptoms you mention seem to have all the hallmarks of a misbehaving subprocess.

      Grats on your Place experiment too, by the way. It's always fun to learn how to respond to excess load on the fly ;)

      Perhaps it's cos he's on mac? I have no idea how chrome generally performs in there as I don't have a mac, but that could potentially be part of it. Also the CPU might be showing higher percentages if it's a little older.
      When I run Chrome on my Windows pc, I see very much the same as you. Very little cpu usage. I also don't see absurd amounts of ram usage either for some reason. I often see people say it's a memory hogging beast, but mine rarely uses more than 100mb. (90% of the time I generally only have 1-3 tabs open.) I only run 2 extensions too, and only one is active all the time.
      I do have Chrome set to use my GPU for rendering though, so that may also contribute to the CPU usage being low?

        At least, I assume he's on mac from the screenshots in the article....

    From Electron's about page:

    In order to keep Electron small (file size) and sustainable (the spread of dependencies and APIs) the project limits the scope of the core project.

    For instance, Electron uses just the rendering library from Chromium rather than all of Chromium. This makes it easier to upgrade Chromium but also means some browser features found in Google Chrome do not exist in Electron.

Join the discussion!

Trending Stories Right Now