Electron is flash for the desktop (2016)
Electron is flash for the desktop

                What is slack doing?



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:



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.



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.

Discussion on hackernews

Discussion on reddit r/programming
            